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);