In PHP, we use the most commonly used to specify HTML tags can be used directly with the Strip_tags function to replace, use it to filter all the HTML tags oh, let me introduce you in addition to this function other methods.
Sometimes we need to save the HTML tab to the database, but some occasions need to take no HTML tag of the pure data, this time will be the HTML tag data processing, the HTML tags are removed. Usually use Htmlspecialchars () to filter the HTML, but the HTML characters escaped, the final display is the HTML source code, the use of strip_tags () can be used to remove HTML tags.
PHP default functions have removed the specified HTML tag, named Strip_tags, which is useful in some situations.
Strip_tags
Strip_tags-strip HTML and PHP tags from a string
String Strip_tags (String str [, String allowable_tags])
Disadvantages:
This function only retains the desired HTML tag, which is the argument string allowable_tags.
The parameters of this function allowable_tags other usages.
The code is as follows |
Copy Code |
Strip_tags ($source, "); Remove the HTML tag so. Strip_tags ($source, ") preserves the div, img, em tags in the string.
|
If you want to remove the specified label for the HTML. Then this function will not meet the requirements.
So I used this function.
The code is as follows |
Copy Code |
function Strip_only_tags ($str, $tags, $stripContent = FALSE) { $content = ";
if (!is_array ($tags)) { $tags = (Strpos ($str, ' > ')!== false? Explode (' > ', Str_replace (' < ', ', $tags)): Array ($tags)); if (end ($tags) = = ") { Array_pop ($tags); } }
foreach ($tags as $tag) { if ($stripContent) { $content = ' (. + |s[^>]*>) |) '; }
$str = Preg_replace (' # |s[^>]*>) '. $content. ' #is ', ', $str); }
return $str; } |
Parameter description
$str-refers to a string that needs to be filtered, such as HTML tags such as div, p, em, IMG, and so on.
$tags-refers to the need to remove the specified HTML tags, such as A, IMG, p, and so on.
$stripContent = false-Removes the contents of the tag, such as deleting the entire link, and defaults to FALSE, which means that the contents of the tag are not deleted.
Instructions for use
The code is as follows |
Copy Code |
$target = Strip_only_tags ($source, Array (' A ', ' em ', ' B ')), remove the A, EM, and B tags within the $source string.
$source = ' This a example fromlixiphp! ';
$target = Strip_only_tags ($source, Array (' A ', ' em '));
Target results This a example from! |
Other options
The code is as follows |
Copy Code |
Remove BR Mark function strip ($STR) { $str =str_replace (" "," ", $str); $str =htmlspecialchars ($STR); Return Strip_tags ($STR); } ?>
|
A custom function
/
The code is as follows |
Copy Code |
** * Remove HTML tags * * @access Public * @param string str * @return String * */ function deletehtml ($STR) { $str = Trim ($STR); Clear spaces on both sides of a string $str = Strip_tags ($str, ""); Use PHP's own functions to clear the HTML format. Keep P Tags $str = Preg_replace ("/t/", "", $str); Use regular expressions to match what needs to be replaced, such as: spaces, wrap, and replace with empty. $str = Preg_replace ("/rn/", "", $str); $str = Preg_replace ("/r/", "", $str); $str = Preg_replace ("/n/", "", $str); $str = Preg_replace ("//", "", $str); $str = Preg_replace ("//", "", $str); Match spaces in HTML Return trim ($STR); return string }
|
http://www.bkjia.com/PHPjc/631529.html www.bkjia.com true http://www.bkjia.com/PHPjc/631529.html techarticle in PHP, we use the most commonly used to specify the HTML tag can be used directly with the Strip_tags function to replace, use it to filter all the HTML tags oh, let me introduce you in addition to this function ...