1. Find all instances of [code]user post text...[/code]
2. If the lang attribute is supplied, keep track of its value for that
code block, e.g. [code lang="css"]
3. Wrap the contents of the code block in HTML tags with the CSS class
set to lang's value if it was specified
4. Of course, output this new string in the same position as before in
the post
Any ideas? Thanks!
Note that I don't need to actually output the text (the software
handles that), just return a $String value.
Here's what I've come up with so far. Any recommended changes? It
seems to do the job pretty well.
$hlString = $String;
$hlString = str_replace('<', '<', $hlString);
$hlString = str_replace('>', '>', $hlString);
$hlString = preg_replace("/\[code( lang=\"([A-Za-z]+)\")?\]/", "<pre
name=\"code\" class=\"$2\">", $hlString);
$hlString = str_replace("[/code]", "</pre>", $hlString);
return $hlString;
the best way to do it is to use regular expressions to find the "lang"
as well as any text inside the [code][/code] tags.
I forgot to note that I needed to convert < and > as well. My little
script works great! CSSForums.org will have syntax highlighting (using
SyntaxHighlighter, see http://code.google.com/p/syntaxhighlighter/)
for HTML, XML, CSS, JavaScript, PHP, and SQL once I finish polishing
this extension and install it this evening.
Sadly, I took a step back: My new Parse method doesn't support
converting newlines to <br />. If I use the usual:
$hlString = str_replace("\r\n", "<br />", $hlString);
My syntax highlighter goes haywire and gets <br />s inserted in place
of [code] newlines. How could I apply that replacement without
replacing "\r\n" contained in [code][/code]? Sounds like another Regex
problem, but these [code] blocks span multiple lines.