Jump to content

escaping html entities outside of html tags


mattgleeson

Recommended Posts

Hi
I need to display html stored in a database on web page but when I display the html I need to convert all html special charaters outside of html tags into html entities.
The php functions htmlentities() etc don't do what I need as they seem to translate ALL applicable symbols to HTML entities. What I need is a function that is a bit smarter in that it only transalates applicable symbols that are OUTSIDE tags into HTML entities. Is there any php or even smarty functions that do this or can you give me some help/tips the best way to achieve this?

Cheers
Something like

[code]
<?php
$str = "<a href='abc.php?x=y&v=w'>Marks & Spencer</a>";

$a = explode ('>', $str);

foreach ($a as $k => $v) {
    if ($v{0} != '<') {
        $b = explode ('<', $v);
        $b[0] = htmlentities($b[0]);
        $a[$k] = join ('<', $b);
    }
}

$newstr = join ('>', $a);

echo $newstr;
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.