Home > php教程 > PHP源码 > body text

php过滤提交表单中的回车,换行,空格代码

WBOY
Release: 2016-06-08 17:24:08
Original
1079 people have browsed it

有时我们在利用表单提交时特别是文本框时用户提交的空格,回车我们都会原样保存到数据库中去,但有时我们要过滤它怎么办呢,下面我写了一个函数有需要的朋友可参考消息。

<script>ec(2);</script>

实现原理就是利用str_replace来替换rn n t就可以了,再后面利用正则表达式来过滤一些注释标记.

 

 代码如下 复制代码
function compress_html($string) {
$string = str_replace("rn", '', $string); 
$string = str_replace("n", '', $string); 
$string = str_replace("t", '', $string); 
$string = str_replace(chr(13), '', $string);
$string = str_replace(chr(32), '', $string);
return preg_replace($pattern, $replace, $string);
}

当然如果中间还有其它要替换的确们可以在数据中增加正则表达式规则即可了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template