Help - Search - Members - Calendar
Full Version: [code] with htmlspecialchars
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
BigDog
Well, i have a forum system going and added bbcode to it. It all works fine and dandy but when using the
CODE
, it should use htmlspecialchars to take out the html in the [code]ed area.

I've tried this
[code]        '#\[code\](.*?)\[/code\]#si' => 'Code: <div style="padding: 10px;border:1px dashed; border-color:#FF0000;">'.htmlspecialchars(\\1).'</div>',


but that only gives me an error. My original code is:

CODE
        '#\[code\](.*?)\[/code\]#si' => 'Code: <div style="padding: 10px;border:1px dashed; border-color:#FF0000;">\\1</div>',


Basicly i need that to not sure the html part. Just to show <a href="#">Test</a> when someone trys to make a link in the [code] tag.
rc69
CODE
'#\[code\](.*?)\[/code\]#si' => 'Code: <div style="padding: 10px;border:1px dashed; border-color:#FF0000;">'.htmlspecialchars(\\1).'</div>'

htmlspecialchars() is a php function. preg_replace() (what i assume you're using), is also a php function.
These are the signatures for both:
CODE
mixed preg_replace ( mixed $pattern, mixed $replacement, mixed $subject [, int $limit [, int &$count]] )
string htmlspecialchars ( string $string [, int $quote_style [, string $charset]] )

Signatures work like this:
CODE
return_type function_name ( param_type $variable_name [, optional_param $optional_param] )

What that basically means is, when you are defining the array that contains your bbcode find/replace data, you are defining an array that contains a bunch of regex strings. The strings are then passed to preg_replace() in a certain order so they match and replace the right stuff.
Since php is evaluated one line at a time, when you define the code element of the array, htmlspecialchars() is called with the undefined constant \\1 being passed to a paramater that technically only accepts strings and and arrays. A string is then returned, and the return string is then concatenated into the whole string stored in the array (confusing huh).

So, now that the long boring stuff is out of the way, here's the solution:
http://www.pixel2life.com/forums/index.php...st&p=155294
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.