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 originalfunction markup($string, $bbcode=true, $smilies=true, $html=false, $rss_mode=false, $links=true) { global $db, $template, $lang; static $random; $string = preg_replace('#(script|about|applet|activex|chrome):#is', '\\1:', $string); // // Needed by some BBCode regexps and smilies // $string = ' '.$string.' '; if ( !$html ) $string = unhtml($string, $rss_mode); if ( $smilies ) { $all_smilies = $template->get_config('smilies'); $full_path = ( $rss_mode ) ? $this->get_config('board_url') : ROOT_PATH; foreach ( $all_smilies as $pattern => $img ) $string = preg_replace('#([^"])('.preg_quote(unhtml ($pattern), '#').')#', '\\1<img src="'.$full_path.'templates/'.$this->get_config('template').'/smilies/'.$img.'" alt="'.unhtml ($pattern).'" />', $string); // // Entity + smiley fix // $string = preg_replace('#(&\#?[a-zA-Z0-9]+)<img src="[^"]+" alt="([^"]+)" />#', '\\1\\2', $string); } if ( $bbcode ) { $string = ' '.$this->bbcode_prepare($string).' '; if ( $this->get_config('target_blank') ) $rel[] = 'external'; if ( $this->get_config('rel_nofollow') ) $rel[] = 'nofollow'; $rel = ( count($rel) ) ? ' rel="'.join(' ', $rel).'"' : ''; // // Protect from infinite loops. // The while loop to parse nested quote tags has the sad side-effect of entering an infinite loop // when the parsed text contains $0 or \0. // Admittedly, this is a quick and dirty fix. For a nice "fix" I refer to the stack based parser in 2.0. // if ( $random == NULL ) $random = $this->random_key(); // // Parse quote tags // // Might seem a bit difficultly done, but trimming doesn't work the usual way // while ( preg_match("#\[quote\](.*?)\[/quote\]#is", $string, $matches) ) { $string = preg_replace("#\[quote\]".preg_quote($matches[1], '#')."\[/quote\]#is", sprintf($template->get_config('quote_format'), $lang['Quote'], ' '.trim($matches[1])).' ', $string); } while ( preg_match("#\[quote=(.*?)\](.*?)\[/quote\]#is", $string, $matches) ) { } // // Undo the dirty fixing. // // // Parse code tags // foreach ( $matches[1] as $oldpart ) { $newpart = preg_replace(array('#<img src="[^"]+" alt="([^"]+)" />#', "#\n#", "#\r#"), array('\\1', '<br />', ''), $oldpart); // replace smiley image tags $string = str_replace('[code]'.$oldpart.'[/code]', '[code]'.$newpart.'[/code]', $string); } $string = preg_replace("#\[code\](.*?)\[/code\]#is", sprintf($template->get_config('code_format'), '\\1'), $string); // // Parse URL's and e-mail addresses enclosed in special characters // if ( $links ) { $ignore_chars = "([^a-z0-9/]|&\#?[a-z0-9]+;)*?"; for ( $i = 0; $i < 2; $i++ ) { "#([\s]".$ignore_chars.")([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)(".$ignore_chars."[\s])#is", "#([\s]".$ignore_chars.")(www\.[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)(".$ignore_chars."[\s])#is", "#([\s]".$ignore_chars.")([a-z0-9&\-_\.\+]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)(".$ignore_chars."[\s])#is" '\\1<a href="\\3" title="\\3"'.$rel.'>\\3</a>\\4', '\\1<a href="http://\\3" title="http://\\3"'.$rel.'>\\3</a>\\4', '\\1<a href="mailto:\\2" title="\\3">\\3</a>\\5' ), $string); } } // // All kinds of BBCode regexps // // [b]text[/b] "#\[b\](.*?)\[/b\]#is" => '<strong>\\1</strong>', // [i]text[/i] "#\[i\](.*?)\[/i\]#is" => '<em>\\1</em>', // [u]text[/u] "#\[u\](.*?)\[/u\]#is" => '<span style="text-decoration:underline">\\1</span>', // [s]text[/s] "#\[s\](.*?)\[/s\]#is" => '<del>\\1</del>', // [left]text[/left] "#\[left\](.*?)\[/left\]#is" => '<div style="text-align:left">\\1</div>', // [right]text[/right] "#\[right\](.*?)\[/right\]#is" => '<div style="text-align:right">\\1</div>', // [center]text[/center] "#\[center\](.*?)\[/center\]#is" => '<div style="text-align:center">\\1</div>', // [img]image[/img] "#\[img\]([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)\[/img\]#is" => ( $links ) ? '<img src="\\1" alt="'.$lang['UserPostedImage'].'" />' : '\\1', // www.usebb.net "#([\s])(www\.[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)#is" => ( $links ) ? '\\1<a href="http://\\2" '.$rel.'>\\2</a>\\3' : '\\1\\2\\3', // ftp.usebb.net "#([\s])(ftp\.[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)([\s])#is" => ( $links ) ? '\\1<a href="ftp://\\2" title="ftp://\\2"'.$rel.'>\\2</a>\\3' : '\\1\\2\\3', // [url]http://www.usebb.net[/url] "#\[url\]([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)\[/url\]#is" => ( $links ) ? '<a href="\\1" '.$rel.'>\\1</a>' : '\\1', // [url=http://www.usebb.net]UseBB[/url] "#\[url=([\w]+?://[\w\#\$%&~/\.\-;:=,\?@\[\]\+\\\\\'!\(\)\*]*?)\](.*?)\[/url\]#is" => ( $links ) ? '<a href="\\1" '.$rel.'>\\2</a>' : '\\2 [\\1]', "#\[mailto\]([a-z0-9&\-_\.\+]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/mailto\]#is" => ( $links ) ? '<a href="mailto:\\1" title="\\1">\\1</a>' : '\\1', "#\[mailto=([a-z0-9&\-_\.\+]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\](.*?)\[/mailto\]#is" => ( $links ) ? '<a href="mailto:\\1" title="\\1">\\3</a>' : '\\3 [\\1]', // [color=red]text[/color] "#\[color=([\#a-z0-9]+)\](.*?)\[/color\]#is" => '<span style="color:\\1">\\2</span>', // [size=999]too big text[/size] "#\[size=([0-9]{3,})\](.*?)\[/size\]#is" => '\\2', // [size=14]text[/size] "#\[size=([0-9]*?)\](.*?)\[/size\]#is" => '<span style="font-size:\\1pt">\\2</span>', // [google=keyword]text[/google] "#\[google=(.*?)\](.*?)\[/google\]#is" => '<a href="http://www.google.com/search?q=\\1"'.$rel.'>\\2</a>', // [youtube]text[/youtube] "#\[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&color=red&fs=0" frameborder="0"></iframe>', ); // // Now parse those regexps // foreach ( $regexps as $find => $replace ) // // Remove tags from attributes // if ( strpos($string, '<') !== false ) { foreach ( $matches[0] as $match ) } } if ( !$html ) { } // // XML (RSS/Atom) does not define elements such as a, pre, etc. // Though, make sure the already escaped < and > are still/double escaped. // if ( $rss_mode ) $string = str_replace(array('<', '>', '<', '>'), array('&lt;', '&gt;', '<', '>'), $string); }
Código PHP:
Ver original/** * Return the BBCode control buttons * * @param bool $links Enable controls for links * @returns string HTML BBCode controls */ function get_bbcode_controls($links=true) { global $lang, $template; array('[b]', '[/b]', 'B', 'font-weight: bold'), array('[i]', '[/i]', 'I', 'font-style: italic'), array('[u]', '[/u]', 'U', 'text-decoration: underline'), array('[s]', '[/s]', 'S', 'text-decoration: line-through'), array('[left]', '[/left]', $lang['Left'], ''), array('[right]', '[/right]', $lang['Right'], ''), array('[center]', '[/center]', $lang['Center'], ''), array('[quote]', '[/quote]', $lang['Quote'], ''), array('[code]', '[/code]', $lang['Code'], ''), ); if ( $links ) { array('[img]', '[/img]', $lang['Img'], ''), array('[url=http://www.example.com]', '[/url]', $lang['URL'], ''), )); } array('[color=red]', '[/color]', $lang['Color'], ''), array('[size=14]', '[/size]', $lang['Size'], ''), array('[youtube]', '[/youtube]', $lang['YouTube'], '') )); foreach ( $controls as $data ) $out[] = '<a href="javascript:void(0);" onclick="insert_tags(\''.$data[0].'\', \''.$data[1].'\')" style="'.$data[3].'">'.$data[2].'</a>'; return join($template->get_config('post_form_bbcode_seperator'), $out); }
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. |