So, regex gives me a headache. It always has, and I've come to accept it and simply avoid using it where I can. Unfortunately, there are times when there simply isn't another good way to do something. I'm in one of those positions now.
Long story short, I'm coding a forum. I'm including some basic bb code and using regex to implement it. One of my BB tags is for code though, and I don't want the other bb codes to apply to anything inside my code tags.
function post_format($pst){
// Convert all special HTML characters into entities to display literally
$pst = htmlentities($pst);
// The array of regex patterns to look for
$format_search = array(
'#\[b\](.*?)\[/b\]#is',
'#\[i\](.*?)\[/i\]#is',
'#\[u\](.*?)\[/u\]#is',
'#\[quote\](.*?)\[/quote\]#is',
'#\[code\](.*?)\[/code\]#is',
'#\[url=((?:ftp|https?)://.*?)\](.*?)\[/url\]#i',
'#\[url\]((?:ftp|https?)://.*?)\[/url\]#i',
);
// The matching array of strings to replace matches with
$format_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<span style="text-decoration: underline;">$1</span>',
'<blockquote class="quote"><b>Quote:</b><br>$1</blockquote>',
'<b>Code:</b><pre class="code-block">$1</pre>',
'<a href="$1">$2</a>',
'<a href="$1">$1</a>',
);
// Perform the actual conversion
$pst = preg_replace($format_search, $format_replace, $pst);
// Convert line breaks in the <br /> tag
$pst = nl2br($pst);
return $pst;
}
Oh, afterthought: I also need the "nl2br" not to apply to section side the code tags.
Any help would be much appreciated.