Goldeneye Posted November 14, 2009 Share Posted November 14, 2009 The background: I have a function that formats text with bold, italics, underlines, images, and automatically anchored URLs. I have a tag (I call it the [raw] tag as in raw-text) for leaving anything inbetween the tags unformatted. The problem: URLs inside the [raw]*[/raw] tags still get automatically anchored. The code: $strs = array('This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you.', '[raw]This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you[/raw]' ); foreach($strs as $str){ preg_match_all('/\[raw\](.*?)\[\/raw\]/si', $str, $code); foreach($code[1] as $stri) $str = '<span style="font-family: monospace; white-space: pre;">'.str_replace('[', '[', $stri).'</span>'; // THIS IS WHERE THE RAW-TAG REPLACEMENT ENDS // ################################## // NOW FOR PARSING THE REGULAR TAGS THAT HAVEN'T HAD THEIR LEFT-SQUARE BRACKETS REPLACED: $search = array('/^[\&\#91\;raw\]]((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$)^[\&\#91\;\/raw\]]/', '/\[i\](.*?)\[\/i\]/si'); $replace = array('<a href="$1" rel="nofollow" target="_blank">$1</a>$4', '<i>$1</i>'); $str = preg_replace($search, $replace, $str); echo '<p>'.$str.'</p>'; } What I expect to happen is that the element of the array will be: This is a <i>string</i> with a link: <a href="http://foobar.com/">http://foobar.com/</a> -- no twix for you. What I expect the second element of the array to look like: This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you The breakdown of my automatically anchoring Regular-Expression: '/^[\&\#91\;raw\]] ((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$) ^[\&\#91\;\/raw\]]/' What this should do is that it looks for URLs not in-between [raw]*[/raw] tags and anchors them. I use ^[\&\#91\;\/raw\]] instead of ^[\[\/raw\]] because the raw-tag works by replacing the first square bracket ([) with it's HTML-entity equivalent ([). A preemptive thanks to any input/advice. Quote Link to comment https://forums.phpfreaks.com/topic/181552-solved-automatically-anchoring-urls-that-are-not-inside-code-tags/ Share on other sites More sharing options...
thebadbad Posted November 16, 2009 Share Posted November 16, 2009 Example: <?php $strings = array( 'This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you.', '[raw]This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you[/raw]' ); //define BB code $replace = array( '~\[i\](.*?)\[/i\]~is' => '<em>$1</em>', '~\b(?:mailto:|(?:https?|ftp|nntp|news)://)\S+~i' => '<a href="$0">$0</a>' ); //define callback function for raw content function raw_convert($str, $reverse = false) { $raw = array( '[' => '&91;', ']' => '&93;', ':' => '&58;' ); if ($reverse) { return str_replace($raw, array_keys($raw), $str); } else { return str_replace(array_keys($raw), $raw, $str); } } foreach ($strings as $string) { //process raw tags $out = preg_replace_callback( '~\[raw\](.*?)\[/raw\]~is', create_function( '$matches', 'return \'[raw]\' . raw_convert($matches[1]) . \'[/raw]\';' ), $string ); //convert BB code $out = preg_replace(array_keys($replace), $replace, $out); //reconvert char substitutes in raw content $out = preg_replace_callback( '~\[raw\](.*?)\[/raw\]~s', create_function( '$matches', 'return raw_convert($matches[1], true);' ), $out ); echo "$out<br /><br />"; } ?> Only drawback I can think of, is that if any of the HTML entities from the raw_convert() function are present inside a pair of [raw] tags, they will be replaced by their character equivalent. But to overcome that you could simply use some other 'unique' tokens instead of their HTML entities. Quote Link to comment https://forums.phpfreaks.com/topic/181552-solved-automatically-anchoring-urls-that-are-not-inside-code-tags/#findComment-958381 Share on other sites More sharing options...
Goldeneye Posted November 16, 2009 Author Share Posted November 16, 2009 Thank you, thebadbad! I had no idea it was more complex than just writing a regular-expression. This sort of task seems like something a reuglar-expression would be great for. But anyway, thank you, again. Quote Link to comment https://forums.phpfreaks.com/topic/181552-solved-automatically-anchoring-urls-that-are-not-inside-code-tags/#findComment-958662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.