note 102620 added to function.strip-tags
| From: | alekseyatfavordotcomdotua at osu1 dot php dot net | Date: | Thu, 24 Feb 2011 21:06:32 +0000 |
| Subject: | note 102620 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177303@lists.php.net to get a copy of this message | ||
strip_tags destroys the whole HTML behind the tags with invalid attributes. Like <img
src="/images/image.jpg""> (look, there is an odd quote before >.)
So I wrote function which fixes unsafe attributes and replaces odd " and ' quotes with
" and '.
function fix_unsafe_attributes($s) {
$out = false;
while (preg_match('/<([A-Za-z])[^>]*?>/', $s, $i, PREG_OFFSET_CAPTURE)) { //
find where the tag begins
$i = $i[1][1]+1;
$out.= substr($s, 0, $i);
$s = substr($s, $i);
// scan attributes and find odd " and '
while (((($i1 = strpos($s, '"')) || 1) && (($i2 = strpos($s,
'\'')) || 1)) && ($i1 !== false || $i2 !== false) &&
(($i = (int)(($i1 !== false) && ($i2 !== false) ? ($i1 < $i2 ? $i1 : $i2) :
($i1 == false ? $i2 : $i1))) !== false) &&
((($c = strpos($s, '>')) === false) || ($i < $c))) {
$c = $s{$i};
if (($i < 1) || ($s{$i-1} != '=')) {
$out.= substr($s, 0, $i).($s{$i} == '"' ? '"' :
'''); // replace odd " and '
$s = substr($s, $i+1);
}else {
$i++;
$out.= substr($s, 0, $i);
$s = substr($s, $i);
if (($i = strpos($s, $c)) !== false) {
$i++;
$out.= substr($s, 0, $i);
$s = substr($s, $i);
}
}
}
}
return $out.$s;
}
Maybe this function can be rewritten with simple regular expression but I have no luck to make it
quickly.
----
Server IP: 213.186.192.247
Probable Submitter: 91.200.180.180
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/102620
Del: integrated -- https://master.php.net/note/delete/102620/integrated
Del: useless -- https://master.php.net/note/delete/102620/useless
Del: bad code -- https://master.php.net/note/delete/102620/bad+code
Del: spam -- https://master.php.net/note/delete/102620/spam
Del: non-english -- https://master.php.net/note/delete/102620/non-english
Del: in docs -- https://master.php.net/note/delete/102620/in+docs
Del: other reasons-- https://master.php.net/note/delete/102620
Reject -- https://master.php.net/note/reject/102620
Search -- https://master.php.net/manage/user-notes.php