I posted a similar thread on this a few days ago and have been beating my head on this issue since then but have made some little progress. I'm trying to do a simple thing - mimic forums like this that display formatted source code. I indicate source code by using tags:
[ code ] ... [ /code ]
I tried a little test on how to format it using this example:
$textstring = "Here is some code:
[ code ]
<?php
echo 'stuff';
?>
[ /code ]
And here's more:
[ code ]
<?php
echo 'more stuff';
?>
[ /code ]
And that wraps it up!";
// I replace the line breaks otherwise the preg_replace doesn't seem to work
$textstring = str_replace("\n","<br>",$textstring);
// preg replace should put anything between [ code ][ /code ] in a HTML table
echo preg_replace("/\[code\](.*)\[\/code\]/", "<table class='code'><tr><td>"."$1"."</td></tr></table>", $textstring);
If you run the code, you'll see even though there are two instances of my code tags, it just starts at the first code tag, skips over the ones in the middle, ending at the closing code tag at the end to find one pattern match. Is there a way to configure my regex so it identifies each individual piece of source code? Or if I'm completely on the wrong track in tackling this issue, any pointers in the right direction would be appreciated :-)