I am putting HTML code into a form field and dropping it into a database. When displayed, I need it to show the actual code. I'm trying to use preg_replace to do this. Here's my code:
$search = array ("'<[\/!]?[<>]?>'si", // Strip out html tags
"'([\r\n])[\s]+'", // Strip out white space
"'&(quot|#34);'i", // Replace html entities
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // evaluate as php
$replace = array ("",
"\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\1)");
$text = preg_replace ($search, $replace, $row['description']);
echo $text;
When I do this, however, it seems only to strip out the HTML and does not replace it with text. Can anyone tell what I'm doing wrong? Is there a better way to do this?
Thanks.