Jump to content

[SOLVED] Preg_replace help


LemonInflux

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

"#\[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>

Link to comment
Share on other sites

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"> &lt;?php echo &quot;lolzorz&quot;; ?&gt;</span> </code></div></div> in /**************/bbcode.php on line 46

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.