There are many ways to filter out some special characters or illegal characters in php. The regular expression str_replace is a good way. It can be used to solve carriage returns, spaces, and newlines.
A system module I was recently responsible for can finally be said to be coming to an end. The remaining work is to cooperate with the testers to test and modify it.
Taking advantage of this relatively free time, I secretly wrote a few blog posts to record the problems encountered during this period and my solutions (let’s talk about solutions).
Okay, without further ado, let’s get to the point.
Those who have this kind of need are usually those who remove the thief program, such as removing the link to the original page. However, what I encountered was that when exporting a customized page to Word, the js code block needed to be removed for safety reasons.
First upload the code
The code is as follows
代码如下 |
复制代码 |
$searchRegex = array(
'/(s*.*?s*)/i', // 超链接
'/[sS]*?/i', // JS脚本代码
);
$replaceStr = array(
'',
''
);
$content = preg_replace($searchRegex, $replaceStr, $content);
|
|
Copy code
|
代码如下 |
复制代码 |
$regexForLink = '/(s*.*?s*)/i'; // 超链接
$regexForJS = '/[sS]*?/i'; // JS脚本代码
|
$searchRegex = array(
'/(s*.*?s*)/i', // Hyperlink
代码如下 |
复制代码 |
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);
}
|
'/[sS]*?/i', // JS script code
);
$replaceStr = array(
'$1',
''
);
$content = preg_replace($searchRegex, $replaceStr, $content);
|
For PHP, this is relatively simple, just use the preg_replace function, not much to say. . . What needs to be paid attention to are two regular rules. This is the key.
The code is as follows
|
Copy code
$regexForLink = '/(s*.*?s*)/i'; // Hyperlink
$regexForJS = '/[sS]*?/i'; // JS script code
If you want to filter HTML codes, spaces, carriage returns and line feeds
The code is as follows
|
Copy code
|
{
$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 strip HTML, XML and PHP tags.
strip_tags(string,allow)
Okay, that’s all, hope it helps next time.
http://www.bkjia.com/PHPjc/633113.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633113.htmlTechArticleThere are many ways to filter some special characters or illegal characters in php, such as the regular expression str_replace A good method, you can use it for carriage returns, spaces, and line breaks...
|
|
|