Ask a Question related to PHP Notes, Design and Development.
-
didou@php.net #1
note 33826 modified in function.strip-tags by didou
To eminate the script tags found in html, don't use the preg_replace approach. The .* can include </script> when there are multiple script tags, and you could be replacing content other than the script tag.
The following function will find the begining and ending character occurance on the script tag, crop the html, then repeat the process until all tags are removed.
function strip_tag_script($html) {
$pos1 = false;
$pos2 = false;
do {
if ($pos1 !== false && $pos2 !== false) {
$first = NULL;
$second = NULL;
if ($pos1 > 0)
$first = substr($html, 0, $pos1);
if ($pos2 < strlen($html) - 1)
$second = substr($html, $pos2);
$html = $first . $second;
}
preg_match("/<script[^>]*>/i", $html, $matches);
$str1 =& $matches[0];
preg_match("/<\/script>/i", $html, $matches);
$str2 =& $matches[0];
$pos1 = strpos($html, $str1);
$pos2 = strpos($html, $str2);
if ($pos2 !== false)
$pos2 += strlen($str2);
} while ($pos1 !== false && $pos2 !== false);
return $html;
}
--was--
To eminate the script tags found in html, don't use the preg_replace approach. The .* can include </script> when there are multiple script tags, and you could be replacing content other than the script tag.
The following function will find the begining and ending character occurance on the script tag, crop the html, then repeat the process until all tags are removed.
function strip_tag_script($html) {
$pos1 = false;
$pos2 = false;
do {
if ($pos1 !== false && $pos2 !== false) {
$first = NULL;
$second = NULL;
if ($pos1 > 0)
$first = substr($html, 0, $pos1);
if ($pos2 < strlen($html) - 1)
$second = substr($html, $pos2 + 1);
$html = $first . $second;
}
preg_match("/<script[^>]*>/i", $html, $matches);
$str1 =& $matches[0];
preg_match("/<\/script>/i", $html, $matches);
$str2 =& $matches[0];
$pos1 = strpos($html, $str1);
$pos2 = strpos($html, $str2);
if ($pos2 !== false)
$pos2 += strlen($str2);
} while ($pos1 !== false && $pos2 !== false);
return $html;
}
[url]http://www.php.net/manual/en/function.strip-tags.php[/url]
didou@php.net Guest
-
note 33993 added to function.strip-tags
Judging from the sheer number of "holes" found in the posted samples, clearly, creating "safe" html is a difficult task. Consider an alternative... -
note 1947 modified in function.sybase-fetch-field by didou
The 'type' field contains (roughly) the datatype of the source column. Types returned are: 'type' Sybase Type ------- ... -
note 19763 modified in function.is-a by didou
php implementation to keep us happy for now if (!function_exists('is_a')) { function is_a($class, $match) { if (empty($class)) { return false;... -
note 33580 added to function.strip-tags
For fixing the <scr<script></script>ipt> bug, wouldn't it be ok to call strip_tags iteratively until the string does not change anymore? ----... -
note 33574 modified in function.ftp-chmod by didou
if ftp_chmod doesn't work, jusdt try this : $ftp_ip="ip_of_my_serv_ftp"; $ftp_login="my_login"; $ftp_pass="my_pass"; $ftp_file="/www/motd.txt";...



Reply With Quote

