PHP uses regular filter links, tags, spaces, line breaks

Source: Internet
Author: User
Tags html tags


Strip_tags function

Strip_tags---To remove html and PHP tags from strings

Syntax: string strip_tags (String str [, String allowable_tags])

Cases

The code is as follows Copy Code

<?php
$text = ' <p>test paragraph.</p><!--Comment--> <a href= ' #fragment ' >other text</a> ';
echo Strip_tags ($text);
echo "n";

Allow <p> and <a>
Echo strip_tags ($text, ' <p><a> ');
?>

Regular filter all kinds of labels, spaces, line feed

The code is as follows Copy Code

$str =preg_replace ("/s+/", "", $str); Filter Excess Carriage return
$str =preg_replace ("/<[]+/si", "<", $str); Filter <__ ("<" with a space after)
$str =preg_replace ("/<!–.*?–>/si", "", $str); Comments
$str =preg_replace ("/<". *?) >/si "," ", $str); Filtration DOCTYPE
$str =preg_replace ("/<" (/?html.*?) >/si "," ", $str); Filter HTML Tags
$str =preg_replace ("/<" (/?head.*?) >/si "," ", $str); Filter Head Label
$str =preg_replace ("/<" (/?meta.*?) >/si "," ", $str); Filter META Tags
$str =preg_replace ("/<" (/?body.*?) >/si "," ", $str); Filter Body Tags
$str =preg_replace ("/<" (/?link.*?) >/si "," ", $str); Filter Link Label
$str =preg_replace ("/<" (/?form.*?) >/si "," ", $str); Filter form Labels
$str =preg_replace ("/cookie/si", "Cookie", $str); Filter Cookie Label
$str =preg_replace ("/<" (applet.*?) > (. *?) < (/applet.*?) >/si "," ", $str); Filter Applet Labels
$str =preg_replace ("/<" (/?applet.*?) >/si "," ", $str); Filter Applet Labels
$str =preg_replace ("/<" (style.*?) > (. *?) < (/style.*?) >/si "," ", $str); Filter style Labels
$str =preg_replace ("/<" (/?style.*?) >/si "," ", $str); Filter style Labels
$str =preg_replace ("/<" (title.*?) > (. *?) < (/title.*?) >/si "," ", $str); Filter title Tags
$str =preg_replace ("/<" (/?title.*?) >/si "," ", $str); Filter title Tags
$str =preg_replace ("/<" (object.*?) > (. *?) < (/object.*?) >/si "," ", $str); Filter Object Labels
$str =preg_replace ("/<" (/?objec.*?) >/si "," ", $str); Filter Object Labels
$str =preg_replace ("/<" (noframes.*?) > (. *?) < (/noframes.*?) >/si "," ", $str); Filter Noframes Label
$str =preg_replace ("/<" (/?noframes.*?) >/si "," ", $str); Filter Noframes Label
$str =preg_replace ("/<" (i?frame.*?) > (. *?) < (/i?frame.*?) >/si "," ", $str); Filter Frame Label
$str =preg_replace ("/<" (/?i?frame.*?) >/si "," ", $str); Filter Frame Label
$str =preg_replace ("/<" (script.*?) > (. *?) < (/script.*?) >/si "," ", $str); Filter script Tags
$str =preg_replace ("/<" (/?script.*?) >/si "," ", $str); Filter script Tags
$str =preg_replace ("/javascript/si", "JavaScript", $STR); Filter script Tags
$str =preg_replace ("/vbscript/si", "VBScript", $STR); Filter script Tags
$str =preg_replace ("/on ([a-z]+) S*=/si", "on\1=", $str); Filter script Tags
$str =preg_replace ("/&#/si", "&#", $str); Filter script Tags

PHP Regular Filter HTML hyperlinks

The code is as follows Copy Code
<?php
echo preg_replace ("/<=href=) ([^>]*) (?=>)/I", "#", "<a href= ' www.111cn.net ' > Hello, click here to see </a>< A href= ' www.111cn.net ' > Hello, click here to see </a> ');
?>

Regular:/(? <=href=) ([^>]*) (?=>)/
(? <=exp) matches the position at the end of exp
(? =exp) match the position in front of exp
this regular match after href= ">" All previous characters not ">"
Example: <a href= ' www.111cn.net ' >
found these characters (URLs) replaced with #, you can remove all the links in HTML.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.