Php clears html superlinks, JS scripts, and carriage return

Source: Internet
Author: User

There are many ways to filter out special or invalid characters in php, such as the regular expression str_replace, which is a good method, such as carriage return, space, line breaks can be solved with it.

A nearly responsible system module is finally coming to an end. The rest of the work is to work with testers to test and modify the module.

During this relatively idle time, I secretly wrote several blog posts to record the problems encountered during this time period and my solutions (let's talk about the solution ).

Okay. Let's get started.

In general, there is a need to go to the thief program, such as removing the link of the original page or something. However, what I encounter is to export the custom page to word. To ensure security, we need to remove the js Code block.

Code first

The Code is as follows: Copy code

$ SearchRegex = array (
'/<*.*? S *> (s *.*? S *) </a>/I ', // hyperlink
'/<Scripts *.*? S *> [sS] *? </Script>/I ', // JS script code
);
$ ReplaceStr = array (
'$1 ',
''
);
$ Content = preg_replace ($ searchRegex, $ replaceStr, $ content );


In php, this is relatively simple, that is, the preg_replace function is used... Note that there are two regular expressions. This is the key.

The Code is as follows: Copy code

$ RegexForLink = '/<*.*? S *> (s *.*? S *) </a>/I '; // hyperlink
$ RegexForJS = '/<scripts *.*? S *> [sS] *? </Script>/I '; // JS script code

If you want to filter HTML code, spaces, and line breaks

The Code is as follows: Copy code


Function DeleteHtml ($ str)
{
$ Str = trim ($ str );
$ Str = strip_tags ($ str ,"");
$ Str = ereg_replace ("t", "", $ str );
$ Str = ereg_replace ("rn", "", $ str );
$ Str = ereg_replace ("r", "", $ str );
$ Str = ereg_replace ("n", "", $ str );
$ Str = ereg_replace ("", "", $ str );
Return trim ($ str );
}


To filter all html tags in html, you can use the strip_tags () function to remove HTML, XML, and PHP tags.

Strip_tags (string, allow)

Okay, that's all. I hope it will be helpful next time.

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.