note 113759 added to function.strip-tags
| From: | sERGE-01 | Date: | Thu, 28 Nov 2013 09:43:32 +0000 |
| Subject: | note 113759 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197645@lists.php.net to get a copy of this message | ||
My strip_tags:
1) Simple removal of all disallowed tags. Broken tags remain unchanged:
<?php
$tags_allowed = "a|b|i|s|u|br";
$in_text =
"<b>Bold</b><table><tr><td>Table</td></tr></table><br><i>Italic>></i><div>Div</div>";
$out_text =
preg_replace('#</?(?!('.$tags_allowed.'))\b([^><]*>)#sim',
"", $in_text);
print "Example 1:<br>";
print htmlentities($out_text)."<br>";
?>
-------------------------------
Example 1:
<b>Bold</b>Table<br><i>Italic>></i>Div
-------------------------------
2) This example leaves all allowed tags and screen the rest of the text with htmlentities()
function:
<?php
// getting all of allowed tags with their offset
if(preg_match_all('#</?('.$tags_allowed.')\b([^><]*>)#sim',
$in_text, $matches, PREG_OFFSET_CAPTURE))
{
$out_text = "";
$ofs = 0;
foreach($matches[0] as $tag)
{
// text before allowed tag
$out_text .= htmlentities(substr($in_text,$ofs,$tag[1]-$ofs), ENT_NOQUOTES, "cp1251");
$out_text .= $tag[0]; // next allowed tag
$ofs = $tag[1] + strlen($tag[0]);
}
// adding end of text
$out_text .= htmlentities(substr($in_text, $ofs), ENT_NOQUOTES, "cp1251");
}
print "Example 2:<br>";
print htmlentities($out_text)."<br>";
?>
-------------------------------
Example 2:
<b>Bold</b><table><tr><td>Table</td></tr></table><br>
<i>Italic>></i><div>Div</div>
-------------------------------
----
Server IP: 192.41.45.150
Probable Submitter: 81.5.80.154
----
Manual Page -- http://php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/113759
Del: integrated -- https://master.php.net/note/delete/113759/integrated
Del: useless -- https://master.php.net/note/delete/113759/useless
Del: bad code -- https://master.php.net/note/delete/113759/bad+code
Del: spam -- https://master.php.net/note/delete/113759/spam
Del: non-english -- https://master.php.net/note/delete/113759/non-english
Del: in docs -- https://master.php.net/note/delete/113759/in+docs
Del: other reasons-- https://master.php.net/note/delete/113759
Reject -- https://master.php.net/note/reject/113759
Search -- https://master.php.net/manage/user-notes.php