note 33826 modified in function.strip-tags by didou

From: Date: Wed, 09 Jul 2003 08:08:49 +0000
Subject: note 33826 modified in function.strip-tags by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-51756@lists.php.net to get a copy of this message
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; } http://www.php.net/manual/en/function.strip-tags.php

« previous php.notes (#51756) next »