Page 1 of 1

[SOLVED] htmlspecialchars question

Posted: Tue Oct 05, 2004 10:49 am
by neophyte
I'm pulling text from a database. The text has some html in it. I need to translate special characters like "&" and "<" that might be in the text while allowing things like <b> and <a href> in the body copy. Originally I had coded it like this:

Code: Select all

<?php
 print htmlspecialchars(nl2br($text));
?>
This doesn't work I get <b> <a href> printed to the screen. Is there a way to do this with out doing some sort of REGEX function to replace special chars?

Thanks

Posted: Tue Oct 05, 2004 11:12 am
by feyd

Code: Select all

str_replace('&lt;b&gt;','&lt;b&gt;',$text);

okay

Posted: Tue Oct 05, 2004 11:26 am
by neophyte
Thanks for the tip Feyd, I worked it out like this...

Code: Select all

<?php $specialchars = array("&" => "&", """ => """);
foreach ($specialchars as $key => $val){
 str_replace($key, $val, $text);
 }

print nl2br($text);
>?

Posted: Wed Oct 06, 2004 3:45 am
by twigletmac
Won't replacing quotes cause some issues with quotes around HTML attributes?

Mac