Php uses regular expressions to filter links, tags, spaces, and line breaks-PHP source code

Source: Internet
Author: User
Tags php regular expression
This article provides a detailed summary of php's use of regular expressions to filter links, tags, spaces, and line breaks. For more information, see. This article provides a detailed summary of php's use of regular expressions to filter links, tags, spaces, and line breaks. For more information, see.

Script ec (2); script


Strip_tags Function

Strip_tags --- Remove HTML and PHP tags from strings

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

Example

The Code is as follows:

$ Text ='

Test paragraph.

Other text ';
Echo strip_tags ($ text );
Echo "n ";

// Allow

And
Echo strip_tags ($ text ,'

');
?>

Regular expressions are used to filter tags, spaces, and line breaks.

The Code is as follows:

$ Str = preg_replace ("/s +/", "", $ str); // filter excess carriage return
$ Str = preg_replace ("/<[] +/si", "<", $ str); // filter <__( "<" followed by a space)
$ Str = preg_replace ("/ /Si "," ", $ str); // comment
$ Str = preg_replace ("/<(!. *?)> /Si "," ", $ str); // filter DOCTYPE
$ Str = preg_replace ("/<(/? Html. *?)> /Si "," ", $ str); // filter html tags
$ Str = preg_replace ("/<(/? Head. *?)> /Si "," ", $ str); // Filter head tags
$ Str = preg_replace ("/<(/? Meta. *?)> /Si "," ", $ str); // filter meta tags
$ Str = preg_replace ("/<(/? Body. *?)> /Si "," ", $ str); // filter the body tag
$ Str = preg_replace ("/<(/? Link. *?)> /Si "," ", $ str); // filter link tags
$ Str = preg_replace ("/<(/? Form. *?)> /Si "," ", $ str); // Filter form labels
$ Str = preg_replace ("/cookie/si", "COOKIE", $ str); // filter COOKIE tags
$ Str = preg_replace ("/<(applet. *?)> (.*?) <(/Applet. *?)> /Si "," ", $ str); // filter the applet tag
$ Str = preg_replace ("/<(/? Applet. *?)> /Si "," ", $ str); // filter the applet tag
$ 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 the title tag
$ Str = preg_replace ("/<(/? Title. *?)> /Si "," ", $ str); // filter the title tag
$ Str = preg_replace ("/<(object. *?)> (.*?) <(/Object. *?)> /Si "," ", $ str); // filter object tags
$ Str = preg_replace ("/<(/? Objec. *?)> /Si "," ", $ str); // filter object tags
$ Str = preg_replace ("/<(noframes. *?)> (.*?) <(/Noframes. *?)> /Si "," ", $ str); // filter noframes tags
$ Str = preg_replace ("/<(/? Noframes. *?)> /Si "," ", $ str); // filter noframes tags
$ Str = preg_replace ("/<(I? Frame. *?)> (.*?) <(/I? Frame. *?)> /Si "," ", $ str); // filter frame labels
$ Str = preg_replace ("/<(/? I? Frame. *?)> /Si "," ", $ str); // filter frame labels
$ 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 Expression filter html hyperlinks

The Code is as follows:
Echo preg_replace ("/(? <= Href =) ([^>] *) (? =>)/I "," # "," Hello, click here to see you, click here to see ");
?>

Regular :/(? <= Href =) ([^>] *) (? => )/
(? <= Exp) match the position behind exp
(? = Exp) match the position before exp
This regular expression matches all non-">" characters before ">" after href =
Example:
Find these characters (URLs) and replace them with # To remove all 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.