LemonInflux Posted December 2, 2007 Share Posted December 2, 2007 I'm trying to do some syntax highlighting within comments on a site. Here's how I'm doing it: <?php function bb($str) { $str = htmlspecialchars($str); // All the default bbcode arrays. $bbcode = array( // Bold '#\[b\](.*?)\[/b\]#si' => '<b>\\1</b>', // Italics '#\[i\](.*?)\[/i\]#si' => '<i>\\1</i>', // Underline '#\[u\](.*?)\[/u\]#si' => '<u>\\1</u>', // Strikethrough '#\[s\](.*?)\[/s\]#si' => '<strike>\\1</strike>', // Font colour '#\[color=(.*?)\](.*?)\[/color\]#si' => '<font color="\\1">\\2</font>', // Blinking Text '#\[bl\](.*?)\[/bl\]#si' => '<blink>\\1</blink>', // Marquee '#\[marquee\](.*?)\[/marquee\]#si' => '<marquee>\\1</marquee>', // Links '#\[url=http://(.*?)\](.*?)\[/url]#si' => '<a href="\\1" target="_blank">\\2</a>', '#\[url\](.*?)\[/url\]#si' => '<a href="\\1" target="_blank">\\1</a>', // Quotes '#\[quote=(.*?)\](.*?)\[/quote\]#si' => '<div class="quotebox">Quote (\\1):<br>\\2</div>', '#\[quote\](.*?)\[/quote\]#si' => '<div class="quotebox">Quote<br>\\1</div>', // Images '#\[img\](.*?)\[/img\]#si' => '<img src="\\1" />', // Email '#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1">\\1</a>', '#\[email=(.*?)\](.*?)\[/email]#si' => '<a href="mailto:\\1" target="_blank">\\2</a>', // Syntax Highlighting '#\[code\](.*?)\[/email\]#si' => '<div class="codebox">Code:<div class="code">'. highlight_string('\\1') .'</div></div>' ); $output = preg_replace(array_keys($bbcode), array_values($bbcode), $str); return $output; } ?> Everything works brilliantly, except the code tags. if I put the following: <?php echo bb("[/code/]<?php echo 'lolzor'; ?>[/code/]"); ?> it outputs: \1 [/code/]<?php echo 'lolzor'; ?>[/code/] Can anyone help? Can I not use highlight_string in this? How would I fix it? PS: Slashes added so SMF doesn't mess up. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 Sorry, topic not solved. Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 1) Add the "e" modifer. You want highlight_string() to be evaluated... 2) You closed the tag with "email" instead of "code". 3) You want the array value to be a string- you don't want highlight_string() to be evaluated yet. So you need to add quotes around that. 4) highlight_string() just outputs what it gets. You need to add the second TRUE parameter here. 5) It's also preferred to use $n instead of \\n. So here's what you should get... "#\[code\](.*?)\[/code\]#sie" => "'<div class=\"codebox\">Code:<div class=\"code\">'. highlight_string(\"$1\", TRUE) .'</div></div>'" I've tested it with this script, and it worked. <?php if(!isset($_POST['submit'])) { echo <<<HTML <form method="POST"> <input type="text" name="test"> <input type="submit" name="submit"> </form> HTML; die; } $str = (get_magic_quotes_gpc()) ? stripslashes($_POST['test']) : $_POST['test']; echo preg_replace("#\[code\](.*?)\[/code\]#sie", "'<div class=\"codebox\">Code:<div class=\"code\">'. highlight_string(\"$1\", TRUE) .'</div></div>'", $str); ?> Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 Heh, sorry. It's a bit early for me. Thanks loads, topic solved. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 EDIT: it works now, but with no apostrophes. They get stripped, it seems. Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 Are you using the line I have added before the preg_reaplce() or not? Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 I am. Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 So try it without that line. Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 No luck. Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 Sorry mate, no idea what's going on. Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 "#\[code\](.*?)\[/code\]#si" => "<div class=\"codebox\">Code:<div class=\"code\">". highlight_string("\\1", TRUE) ."</div></div>" If I use this, it outputs Code: <?php echo "lolzorz"; ?> but with no highlighting. source code: <div class="codebox">Code:<div class="code"><code><span style="color: #000000"> <?php echo "lolzorz"; ?></span> </code></div></div> Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 Did you totally ignore what I wrote? You must use the "e" modifer. Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 I tried what you wrote, but you said you had no idea, so I tried to use it myself. When including the e modifier, outputs: Parse error: syntax error, unexpected '<' in /******/bbcode.php(46) : regexp code on line 1 Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: <div class="codebox">Code:<div class="code"><code><span style="color: #000000"> <?php echo "lolzorz"; ?></span> </code></div></div> in /**************/bbcode.php on line 46 Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 That's not exactly what I wrote.. This way: "#\[code\](.*?)\[/code\]#sie"=>"'<div class=\"codebox\">Code:<div class=\"code\">'.highlight_string(\"$1\", TRUE).'</div></div>'" Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 Shown: Code: <?php echo "lolzorz"; ?> source: <div class="codebox">Code:<div class="code"><code><span style="color: #000000"> <?php echo "lolzorz"; ?></span> </code></div></div> Quote Link to comment Share on other sites More sharing options...
Orio Posted December 2, 2007 Share Posted December 2, 2007 Ok, so try it this way: "#\[code\](.*?)\[/code\]#sie"=>"'<div class=\"codebox\">Code:<div class=\"code\">'.highlight_string(html_entity_decode(\"$1\"), TRUE).'</div></div>'" Orio. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted December 2, 2007 Author Share Posted December 2, 2007 Perfect! Thanks ever so much, Orio! Topic solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.