# [web] UBB style codes, RegEx Help.

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, I'm working on a very, VERY simple UBB-style code parser for my site. I don't need anything elaborate like a forum would have, just a few ways to spice up some articles and comments. So this is what I've got:
		// Bold
$szText = preg_replace("/$b$(.*?)$\/b$/sim", "<span class=\"bold\">$1</span>", $szText); // Italics$szText = preg_replace("/$i$(.*?)$\/i$/sim", "<span class=\"italic\">$1</span>",$szText);
// Underline
$szText = preg_replace("/$u$(.*?)$\/u$/sim", "<span class=\"underline\">$1</span>", $szText); // Code$szText = preg_replace(NO IDEA WHAT TO PUT HERE, OR HERE, $szText);$szText = preg_replace("/$code$(.*?)$\/code$/sim", "<pre>$1</pre>",$szText);


I'm struggling on one thing though, and that's with the [ code ] [ /code ] tags. See the first rule? NO IDEA WHAT TO PUT HERE, what I'm trying to do is remove all <br />s, before I parse it. I need to do this to retain proper styling, as it's already been nl2br'd. So this: [ code ]Batman<br />\nSuperman[ /code ] would become: [ code ]Batman\nSuperman[ /code ] Anyone have any idea on how I can remove the <br />s from within a [ code ] [ /code ] WITHOUT str_replacing them all away (as obviously, there might be more than just a code section, and I want to retain those <br />s.

##### Share on other sites
The easiest thing is probabely a preg_match() to return an array containing only the text portions that are between the code tags. Then loop the array with br2ln() and then replace the original code block contents with the new ones using preg_replace().

Not sure if this is 100% correct, but here goes:

//lift all the code sectionspreg_match("/$code$(.*?)$\/code$/sim", $szText,$array);//$array[0] will contain the full match,$array[1] and higher the code blocksfor($i = 1;$i<count($array);$i++){  $array_new[$i] = br2nl($array[$i]);  $array_old[$i] = preg_quote($array[$i]);}//now replace the blocks in the original messagepreg_replace($array_old,$array_new, $szText); #### Share this post ##### Link to post ##### Share on other sites What I've done in my evoWeb system (example) is use the regex match to find the location of all code boxes and extract the contents into an array. That instance of the code box is then replaced with a random placeholder string and the rest of the page is parsed. From there, the codeboxes are reinserted into the processed page (using a string match) and dumped out to the response buffer. This allows me to strip out any element and reinsert it after the main processing has occurred. It works quite well, I think. #### Share this post ##### Link to post ##### Share on other sites Thanks guys, I've sort of merged your techniques. Here's what I'm using:  // Code preg_match("/$code$(.*?)$\/code$/sim",$szText, $szMatches); for($iLoop = 1; $iLoop < count($szMatches); ++$iLoop)$szText = str_replace($szMatches[$iLoop], str_replace("<br />", "", $szMatches[$iLoop]), $szText);$szText = preg_replace("/$code$(.*?)$\/code$/sim", "<pre>$1</pre>",$szText);

Works like a charm, thanks a lot! =)

##### Share on other sites
I recently discovered regex, and I love it already! It owns. =)