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.