It would be nice if I could call my_escape($text, '<p><b><i>') that escapes everything except all <p>, <b> and <i> tags. I'm looking for a generic solution where I can specify an arbitrary set of tags. Does this exist? If not what's the best approach to implement it?
in htmlspecialchars function it converts html tags to
& to &
" to "
' to '
< to <
> to >
after convert you can do reverse to decode
<?php
$test="<p><b><a>Test</b></a></p>";
$test = htmlspecialchars($test);
$test = str_replace("<p>", "<p>", $test);
$test = str_replace("<i>", "<i>", $test);
$test = str_replace("<b>", "<b>", $test);
$test = str_replace("</b>", "</b>", $test);
$test = str_replace("</i>", "</i>", $test);
$test = str_replace("</p>", "</p>", $test);
echo $test;
?>
Your best bet is to do something like this
// Add placeholders
$search = array('<p>', '<b>');
$replace = array("\ap\a", "\ab\a");
$text = str_replace($search, $replace, $text);
$text = htmlspecialchars($text);
// Put it all back together
$text = str_replace($replace, $search, $text);
It would be best to use a regular expression, but that is a lot more explaining.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With