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.

    Fooglmog wrote:

    Unfortunately, there are times when there simply isn't another good way to do something. I'm in one of those positions now.

    Well ... you could write a tokenising parser, which is what a lot of forum software uses.

    But here's something I cobbled together once for a giggle.

      You could use my approach as well, which is to find the tags which should have sub tags parsed, in this case code. So you find the code and find the string you would place in there in its place, then you do your other codes and after those are done you go back and find code again, this time finding the strings to replace and replacing them with the strings you found much earlier. Have a look at this class I made for it, hope you understand and it helps!

      Remove the .txt extension and you'll also notice at the bottom is an example of it being used...

        Weedpacket, thank you kindly. That's exactly what I was asking for.

        Derokorian -- that is absolutely beautiful. It's going to take me a while to break down the steps involved in that code so I can produce something similar -- but that's so much better than what I was going to do that I have to do it that way now. 🙂

          Write a Reply...