Re: HTML Character Parsing
| From: | Julio Nobrega | Date: | Fri, 12 Apr 2002 00:41:32 +0000 |
| Subject: | Re: HTML Character Parsing | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-92684@lists.php.net to get a copy of this message | ||
I have this class, comes close to what you want. Feel free to modify,
sorry I didn't have the time to comment it yet. I hope you can understand
and use it :-) Anyway test it and see what it does.
class InseForm
{
function AllowedTags()
{
$allowed_tags = Array();
$allowed_tags['<a href>'] = '</a>';
$allowed_tags['<b>'] = '</b>';
$allowed_tags[] = '<br>';
$allowed_tags['<i>'] = '</i>';
$allowed_tags['<p>'] = '</p>';
return $allowed_tags;
}
function FilterTags($text)
{
$text = strip_tags($text, '<a><b><br><i><p>');
$text = ereg_replace("<a[^>]+href *= *([^ ]+)[^>]*>", "<a
href=\\1>", $text);
$text = ereg_replace("<(/?[[b]r?|i|p|a])[^>]*>", "<\\1>",
$text);
return htmlentities(stripslashes(trim($text)));
}
function CountAllowedTags($text)
{
$missing_tag = '';
foreach($this->AllowedTags() as $open_tag => $close_tag) {
$nr_open_tags = substr_count(strtolower($text), $open_tag);
$nr_close_tags = substr_count(strtolower($text), $close_tag);
if ($nr_open_tags > $nr_close_tags) {
$missing_tag .= 'A tag ' . htmlentities($open_tag) . ' was
not closed!<br />';
}
}
if (!ereg("^[[:blank:]]*$", $missing_tag)) {
return '<br /><span style="color:red;">' . $missing_tag
.
'</span>';
} else {
return false;
}
}
}
--
Julio Nobrega.
Tô chegando:
http://www.inerciasensorial.com.br
"David" <davidr@execulink.co.za> wrote in message
news:20020411130759.38268.qmail@pb1.pair.com...
> Hi all,
>
> I have a textarea which will containg info from the user. This then
> needs to be parsed through something like htmlspecialchars() or
> htmlentities().
>
> The issue is that my system really needs to do the following:
>
> 1. Accept the info
> 2. Check if there is any HTML syntax (<p>, etc)
> 3. If YES: remove anything that might be harmful (eg FORM, etc)
> 4. If NO: Add replace CR/LF with <BR>
>
> The idea is that normal formatting such as <b>, <i>, <u>, <a href> is
> ok, but I do not want off illegal stuff. I want something a little like
> Slashdot's stuff.
>
> Easy?? anyone know anything about this?
>
> Thanks
>
> David R
>
>