php过滤回车
⑴ php 去除回车符
<?php
$urlpath="15442447
";
echo $urlpath.'<br>';
$urlpath = trim($urlpath);
echo $urlpath;
echo '这里';
?>
运行后查看源代码,只有在源代码下才能看到具体有没有清除回车。
⑵ php中数据过滤的问题
我来解释一下吧
preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string);
去掉控制字符,你google一下ascii table就知道了,php里面 - 代表范围,比如\x00-\x08指的是ASCII代码在\x00到\x08范围的字符,\x0A和\x0D代表回车换行,所以没包含在这个里面,否则直接\x00-\x1F了,
$string = str_replace(array("\0","%00","\r"),'',$string);
\0表示ASCII 0x00的字符,通常作为字符串结束标志
$string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string);
我们知道HTML里面可以用xx;来对一些字符进行编码,比如 (空格), ߷ Unicode字符等,A(?!B) 表示的是A后面不是B,所以作者想保留 ߷类似的 HTML编码字符,去掉其他的问题字符,比如 &123; nbsp;
str_replace(array("%3C",'<'),'<',$string);
第一个'<'多余吧,%3C是编码以后的 <, 一般用在URL编码里
str_replace(array("%3E",'>'),'>',$string);
str_replace(array('"',"'","\t",' '),array('"',"'",'',''),$string);
略过
有问题再追问
⑶ php 去掉回车符号空格符号还有反斜杠的函数
针对字符可以用 str_replace($search, $replace,$str [, int &count] );
针对HTML标签可以用 htmlspecialchars ( string string [, int quote_style [, string charset]] )
用法可以在PHP手册上查一下。
⑷ php 过滤掉html标签及标签内的所有内容
方法一:使用strip_tags()函数
strip_tags() 函数剥去字符串中的 HTML、XML 以及PHP的标签。
使用案例:
$string = "<p>这里是潘旭博客</p>"
$newStr = strip_tags($string);
echo $newStr;
方法二:使用str_replace()函数
str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)
使用案例:
$string = "<p>这里是潘旭博客</p>";
$newStr = str_replace(array("<p>","</p>"),array("",""));
echo $newStr;
另外还有一种是通过正则的方法,请参考:https://panxu.net/article/8385.html
⑸ 我做了一个留言板现在想用PHP过滤代码,应该怎么做呀详细思路,谢谢!
function filterhtml($str)
{
$str=stripslashes($str);
$str=preg_replace("/\s+/", ' ', $str); //过滤多余回车
$str=preg_replace("/<[ ]+/si",'<',$str); //过滤<__("<"号后面带空格)
$str=preg_replace("/<\!--.*?-->/si",'',$str); //注释
$str=preg_replace("/<(\!.*?)>/si",'',$str); //过滤DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si",'',$str); //过滤html标签
$str=preg_replace("/<(\/?head.*?)>/si",'',$str); //过滤head标签
$str=preg_replace("/<(\/?meta.*?)>/si",'',$str); //过滤meta标签
$str=preg_replace("/<(\/?body.*?)>/si",'',$str); //过滤body标签
$str=preg_replace("/<(\/?link.*?)>/si",'',$str); //过滤link标签
$str=preg_replace("/<(\/?form.*?)>/si",'',$str); //过滤form标签
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si",'',$str); //过滤applet标签
$str=preg_replace("/<(\/?applet.*?)>/si",'',$str); //过滤applet标签
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si",'',$str); //过滤style标签
$str=preg_replace("/<(\/?style.*?)>/si",'',$str); //过滤style标签
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si",'',$str); //过滤title标签
$str=preg_replace("/<(\/?title.*?)>/si",'',$str); //过滤title标签
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si",'',$str); //过滤object标签
$str=preg_replace("/<(\/?objec.*?)>/si",'',$str); //过滤object标签
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si",'',$str); //过滤noframes标签
$str=preg_replace("/<(\/?noframes.*?)>/si",'',$str); //过滤noframes标签
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si",'',$str); //过滤frame标签
$str=preg_replace("/<(\/?i?frame.*?)>/si",'',$str); //过滤frame标签
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si",'',$str); //过滤script标签
$str=preg_replace("/<(\/?script.*?)>/si",'',$str); //过滤script标签
$str=preg_replace("/javascript/si","JAVASCRIPT",$str); //过滤script标签
$str=preg_replace("/vbscript/si","VBSCRIPT",$str); //过滤script标签
$str=preg_replace("/on([a-z]+)\s*=/si","ON\\1=",$str); //过滤script标签
$str=preg_replace("//si","&#",$str); //过滤script标签,如javAsCript:alert('aabb)
$str=addslashes($str);
return($str);
}
⑹ 用php过滤html部分标签
$str=preg_replace("/\s+/", " ", $str); //过滤多余回车
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)
$str=preg_replace("/<\!--.*?-->/si","",$str); //注释
$str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str); //过滤html标签
$str=preg_replace("/<(\/?head.*?)>/si","",$str); //过滤head标签
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //过滤meta标签
$str=preg_replace("/<(\/?body.*?)>/si","",$str); //过滤body标签
$str=preg_replace("/<(\/?link.*?)>/si","",$str); //过滤link标签
$str=preg_replace("/<(\/?form.*?)>/si","",$str); //过滤form标签
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(\/?style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(\/?title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/<(\/?script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签
$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //过滤script标签
$str=preg_replace("//si","&#",$str); //过滤script标签,如javAsCript:alert(
清除空格,换行
function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}
过滤HTML属性
1,过滤所有html标签的正则表达式:
复制代码 代码如下:
</?[^>]+>
//过滤所有html标签的属性的正则表达式:
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);
3,过滤部分html标签的正则表达式的排除式(比如排除<p>,即不过滤<p>):
复制代码 代码如下:
</?[^pP/>]+>
4,过滤部分html标签的正则表达式的枚举式(比如需要过滤<a><p><b>等):
复制代码 代码如下:
</?[aApPbB][^>]*>
5,过滤部分html标签的属性的正则表达式的排除式(比如排除alt属性,即不过滤alt属性):
复制代码 代码如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,过滤部分html标签的属性的正则表达式的枚举式(比如alt属性):
复制代码 代码如下:
(\s)alt=[^\s]*
⑺ php 如何去除回车换行符
php 去除回车换行符有三种方案:
<?php
//php不同系统的换行
//不同系统之间换行的实现是不一样的
//linux与unix中用
//MAC用
//window为了体现与linux不同则是
//所以在不同平台上实现方法就不一样
//php有三种方法来解决
//1、使用str_replace来替换换行
$str=str_replace(array(" "," "," "),"",$str);
//2、使用正则替换
$str=preg_replace('//s*/','',$str);
//3、使用php定义好的变量(建议使用)
$str=str_replace(PHP_EOL,'',$str);
?>
⑻ ajax传过来的值如果有换行符 用php过滤换行符过滤不掉
我试验过,ajax提交的应该是 , 你可以过滤全部的换行符:
str_replace(array(" "," "," "),'',$_POST['addition1'])
⑼ php字符串处理问题,如何去掉文本域中输入的回车
php用两个函数可以替换。
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
这两个函数中,第一个函数较为高效,第二个函数使用了正则表达式替换,效率会低点,
但是替换更加灵活。
例子:
<?php
$str="文本域中输入的内容";
//单个字符串替换
str_replace(" ","",$str);
preg_replace("/ /","",$str);
//多个字符串替换
str_replace(array(" "," "),"",$str);
str_replace(array(" "," "),array("",""),$str);
preg_replace('/ | /',"",$str);
⑽ PHP 去除连续的空格 换行 回车 怎么弄
去掉连续的意思是必须是两个或者两个以上才去掉么?
$str="abd c";
echopreg_replace('#s{2,}#','',$str);//outputabdc
去掉所有空白字符:
preg_replace('#s#','',$str);
多个连续空白字符替换成一个空格:
preg_replace('#s+#','',$str);