Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/09/2012, 19:35
Lokitozzz
 
Fecha de Ingreso: junio-2009
Mensajes: 138
Antigüedad: 14 años, 10 meses
Puntos: 4
API adf.ly & expresiones regulares con codigos BB

Buenas otra vez aquí.. Tengo estas dos funciones, del sistema de foros UseBB donde se parsean los codigos BB con expresiones regulares & quiero agregarle la API de adf.ly para reemplazar solo las urls que se ingresen.


Código PHP:
Ver original
  1. function markup($string, $bbcode=true, $smilies=true, $html=false, $rss_mode=false, $links=true) {
  2.        
  3.         global $db, $template, $lang;
  4.         static $random;
  5.        
  6.         $string = preg_replace('#(script|about|applet|activex|chrome):#is', '\\1:', $string);
  7.        
  8.         //
  9.         // Needed by some BBCode regexps and smilies
  10.         //
  11.         $string = ' '.$string.' ';
  12.        
  13.         if ( !$html )
  14.             $string = unhtml($string, $rss_mode);
  15.        
  16.         if ( $smilies ) {
  17.            
  18.             $all_smilies = $template->get_config('smilies');
  19.             krsort($all_smilies);
  20.             $full_path = ( $rss_mode ) ? $this->get_config('board_url') : ROOT_PATH;
  21.            
  22.             foreach ( $all_smilies as $pattern => $img )
  23.                 $string = preg_replace('#([^"])('.preg_quote(unhtml($pattern), '#').')#', '\\1<img src="'.$full_path.'templates/'.$this->get_config('template').'/smilies/'.$img.'" alt="'.unhtml($pattern).'" />', $string);
  24.            
  25.             //
  26.             // Entity + smiley fix
  27.             //
  28.             $string = preg_replace('#(&\#?[a-zA-Z0-9]+)<img src="[^"]+" alt="([^"]+)" />#', '\\1\\2', $string);
  29.            
  30.         }
  31.        
  32.         if ( $bbcode ) {
  33.            
  34.             $string = ' '.$this->bbcode_prepare($string).' ';
  35.            
  36.             $rel = array();
  37.             if ( $this->get_config('target_blank') )
  38.                 $rel[] = 'external';
  39.             if ( $this->get_config('rel_nofollow') )
  40.                 $rel[] = 'nofollow';
  41.             $rel = ( count($rel) ) ? ' rel="'.join(' ', $rel).'"' : '';
  42.            
  43.             //
  44.             // Protect from infinite loops.
  45.             // The while loop to parse nested quote tags has the sad side-effect of entering an infinite loop
  46.             // when the parsed text contains $0 or \0.
  47.             // Admittedly, this is a quick and dirty fix. For a nice "fix" I refer to the stack based parser in 2.0.
  48.             //
  49.             if ( $random == NULL )
  50.                 $random = $this->random_key();
  51.            
  52.             $string = str_replace(array('$', "\\"), array('$'.$random, '\'.$random), $string);
  53.            
  54.             //
  55.             // Parse quote tags
  56.             //
  57.             // Might seem a bit difficultly done, but trimming doesn't work the usual way
  58.             //
  59.             while ( preg_match("#\[quote\](.*?)\[/quote\]#is", $string, $matches) ) {
  60.  
  61.                 $string = preg_replace("#\[quote\]".preg_quote($matches[1], '#')."\[/quote\]#is", sprintf($template->get_config('quote_format'), $lang['Quote'], ' '.trim($matches[1])).' ', $string);
  62.                 unset($matches);
  63.  
  64.             }
  65.             while ( preg_match("#\[quote=(.*?)\](.*?)\[/quote\]#is", $string, $matches) ) {
  66.  
  67.                 $string = preg_replace("#\[quote=".preg_quote($matches[1], '#')."\]".preg_quote($matches[2], '#')."\[/quote\]#is", sprintf($template->get_config('quote_format'), sprintf($lang['Wrote'], $matches[1]), ' '.trim($matches[2]).' '), $string);
  68.                 unset($matches);
  69.  
  70.             }
  71.            
  72.             //
  73.             // Undo the dirty fixing.
  74.             //
  75.             $string = str_replace(array('$'.$random, '\'.$random), array('$', "\\"), $string);
  76.            
  77.             //
  78.             // Parse code tags
  79.             //
  80.             preg_match_all("#\[code\](.*?)\[/code\]#is", $string, $matches);               
  81.             foreach ( $matches[1] as $oldpart ) {
  82.                
  83.                 $newpart = preg_replace(array('#<img src="[^"]+" alt="([^"]+)" />#', "#\n#", "#\r#"), array('\\1', '<br />', ''), $oldpart); // replace smiley image tags
  84.                 $string = str_replace('[code]'.$oldpart.'[/code]', '[code]'.$newpart.'[/code]', $string);
  85.                
  86.             }
  87.             $string = preg_replace("#\[code\](.*?)\[/code\]#is", sprintf($template->get_config('code_format'), '\\1'), $string);
  88.            
  89.             //
  90.             // Parse URL's and e-mail addresses enclosed in special characters
  91.             //
  92.             if ( $links ) {
  93.  
  94.                 $ignore_chars = "([^a-z0-9/]|&\#?[a-z0-9]+;)*?";
  95.                 for ( $i = 0; $i < 2; $i++ ) {
  96.  
  97.                     $string = preg_replace(array(
  98.                         "#([\s]".$ignore_chars.")([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)(".$ignore_chars."[\s])#is",
  99.                         "#([\s]".$ignore_chars.")(www\.[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)(".$ignore_chars."[\s])#is",
  100.                         "#([\s]".$ignore_chars.")([a-z0-9&\-_\.\+]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)(".$ignore_chars."[\s])#is"
  101.                     ), array(
  102.                         '\\1<a href="\\3" title="\\3"'.$rel.'>\\3</a>\\4',
  103.                         '\\1<a href="http://\\3" title="http://\\3"'.$rel.'>\\3</a>\\4',
  104.                         '\\1<a href="mailto:\\2" title="\\3">\\3</a>\\5'
  105.                     ), $string);
  106.  
  107.                 }
  108.  
  109.             }
  110.            
  111.             //
  112.             // All kinds of BBCode regexps
  113.             //
  114.             $regexps = array(
  115.                 // [b]text[/b]
  116.                     "#\[b\](.*?)\[/b\]#is" => '<strong>\\1</strong>',
  117.                 // [i]text[/i]
  118.                     "#\[i\](.*?)\[/i\]#is" => '<em>\\1</em>',
  119.                 // [u]text[/u]
  120.                     "#\[u\](.*?)\[/u\]#is" => '<span style="text-decoration:underline">\\1</span>',
  121.                 // [s]text[/s]
  122.                     "#\[s\](.*?)\[/s\]#is" => '<del>\\1</del>',
  123.                 // [left]text[/left]
  124.                     "#\[left\](.*?)\[/left\]#is" => '<div style="text-align:left">\\1</div>',
  125.                 // [right]text[/right]
  126.                     "#\[right\](.*?)\[/right\]#is" => '<div style="text-align:right">\\1</div>',
  127.                 // [center]text[/center]
  128.                     "#\[center\](.*?)\[/center\]#is" => '<div style="text-align:center">\\1</div>',
  129.                 // [img]image[/img]
  130.                     "#\[img\]([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)\[/img\]#is" => ( $links ) ? '<img src="\\1" alt="'.$lang['UserPostedImage'].'" />' : '\\1',
  131.                 // www.usebb.net
  132.                     "#([\s])(www\.[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)#is" => ( $links ) ? '\\1<a href="http://\\2" '.$rel.'>\\2</a>\\3' : '\\1\\2\\3',
  133.                 // ftp.usebb.net
  134.                     "#([\s])(ftp\.[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)([\s])#is" => ( $links ) ? '\\1<a href="ftp://\\2" title="ftp://\\2"'.$rel.'>\\2</a>\\3' : '\\1\\2\\3',
  135.                 // [url]http://www.usebb.net[/url]
  136.                     "#\[url\]([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)\[/url\]#is" => ( $links ) ? '<a href="\\1" '.$rel.'>\\1</a>' : '\\1',
  137.                 // [url=http://www.usebb.net]UseBB[/url]
  138.                     "#\[url=([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)\](.*?)\[/url\]#is" => ( $links ) ? '<a href="\\1" '.$rel.'>\\2</a>' : '\\2 [\\1]',
  139.                 // [mailto][email protected][/mailto]
  140.                     "#\[mailto\]([a-z0-9&\-_\.\+]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/mailto\]#is" => ( $links ) ? '<a href="mailto:\\1" title="\\1">\\1</a>' : '\\1',
  141.                 // [[email protected]]mail me[/mailto]
  142.                     "#\[mailto=([a-z0-9&\-_\.\+]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\](.*?)\[/mailto\]#is" => ( $links ) ? '<a href="mailto:\\1" title="\\1">\\3</a>' : '\\3 [\\1]',
  143.                 // [color=red]text[/color]
  144.                     "#\[color=([\#a-z0-9]+)\](.*?)\[/color\]#is" => '<span style="color:\\1">\\2</span>',
  145.                 // [size=999]too big text[/size]
  146.                     "#\[size=([0-9]{3,})\](.*?)\[/size\]#is" => '\\2',
  147.                 // [size=14]text[/size]
  148.                     "#\[size=([0-9]*?)\](.*?)\[/size\]#is" => '<span style="font-size:\\1pt">\\2</span>',
  149.                 // [google=keyword]text[/google]
  150.                     "#\[google=(.*?)\](.*?)\[/google\]#is" => '<a href="http://www.google.com/search?q=\\1"'.$rel.'>\\2</a>',
  151.                 // [youtube]text[/youtube]
  152.                     "#\[youtube\](.*?)\[/youtube\]#is" => '<iframe width="450" height="299" style="padding: 5px; background-color: #FFF; border: 1px solid silver;" src="http://www.youtube.com/embed/\\1?theme=light&amp;color=red&amp;fs=0" frameborder="0"></iframe>',
  153.             );
  154.            
  155.             //
  156.             // Now parse those regexps
  157.             //
  158.             foreach ( $regexps as $find => $replace )
  159.                 $string = preg_replace($find, $replace, $string);
  160.            
  161.             //
  162.             // Remove tags from attributes
  163.             //
  164.             if ( strpos($string, '<') !== false ) {
  165.    
  166.                 preg_match_all('#[a-z]+="[^"]*<[^>]*>[^"]*"#', $string, $matches);
  167.    
  168.                 foreach ( $matches[0] as $match )
  169.                     $string = str_replace($match, strip_tags($match), $string);
  170.    
  171.             }
  172.            
  173.         }
  174.        
  175.         if ( !$html ) {
  176.            
  177.             $string = str_replace("\n", "<br />", $string);
  178.             $string = str_replace("\r", "", $string);
  179.            
  180.         }
  181.        
  182.         //
  183.         // XML (RSS/Atom) does not define elements such as a, pre, etc.
  184.         // Though, make sure the already escaped < and > are still/double escaped.
  185.         //
  186.         if ( $rss_mode )
  187.             $string = str_replace(array('&lt;', '&gt;', '<', '>'), array('&amp;lt;', '&amp;gt;', '&lt;', '&gt;'), $string);
  188.        
  189.         return trim($string);
  190.        
  191.     }

Código PHP:
Ver original
  1. /**
  2.      * Return the BBCode control buttons
  3.      *
  4.      * @param bool $links Enable controls for links
  5.      * @returns string HTML BBCode controls
  6.      */
  7.     function get_bbcode_controls($links=true) {
  8.        
  9.         global $lang, $template;
  10.        
  11.         $controls = array(
  12.             array('[b]', '[/b]', 'B', 'font-weight: bold'),
  13.             array('[i]', '[/i]', 'I', 'font-style: italic'),
  14.             array('[u]', '[/u]', 'U', 'text-decoration: underline'),
  15.             array('[s]', '[/s]', 'S', 'text-decoration: line-through'),
  16.             array('[left]', '[/left]', $lang['Left'], ''),
  17.             array('[right]', '[/right]', $lang['Right'], ''),
  18.             array('[center]', '[/center]', $lang['Center'], ''),
  19.             array('[quote]', '[/quote]', $lang['Quote'], ''),
  20.             array('[code]', '[/code]', $lang['Code'], ''),
  21.         );
  22.  
  23.         if ( $links ) {
  24.  
  25.             $controls = array_merge($controls, array(
  26.                 array('[img]', '[/img]', $lang['Img'], ''),
  27.                 array('[url=http://www.example.com]', '[/url]', $lang['URL'], ''),
  28.             ));
  29.  
  30.         }
  31.  
  32.         $controls = array_merge($controls, array(
  33.             array('[color=red]', '[/color]', $lang['Color'], ''),
  34.             array('[size=14]', '[/size]', $lang['Size'], ''),
  35.             array('[youtube]', '[/youtube]', $lang['YouTube'], '')
  36.         ));
  37.        
  38.         $out = array();
  39.         foreach ( $controls as $data )
  40.             $out[] = '<a href="javascript:void(0);" onclick="insert_tags(\''.$data[0].'\', \''.$data[1].'\')" style="'.$data[3].'">'.$data[2].'</a>';
  41.        
  42.         return join($template->get_config('post_form_bbcode_seperator'), $out);
  43.        
  44.     }

La API de adf.ly no es mas que esto, & hacer un llamado con file_get_contents:

http://api.adf.ly/api.php?key=XXX&uid=XXX&advert_type=int&domain=adf.ly&url=http://somewebsite.com

Pero el problema es que no se como reemplazar las urls..
Como lo puedo hacer..?

De antemano, gracias.