❶ php過濾非法字元
幫你寫了個函數,要用時,調用一下就可以了,希望對你有幫組
function safe_string($str){ //過濾安全字元
$str=str_replace("'","",$str);
$str=str_replace('"',"",$str);
$str=str_replace(" ","$nbsp;",$str);
$str=str_replace("\n;","<br/>",$str);
$str=str_replace("<","<",$str);
$str=str_replace(">",">",$str);
$str=str_replace("\t"," ",$str);
$str=str_replace("\r","",$str);
$str=str_replace("/[\s\v]+/"," ",$str);
return $str;
}
❷ 求一個php簡單的過濾除<br>,<p>,<style>html標簽的正則或方法
針對你這個<a>123</a>的例子的
$a=<<<str
<a>123</a>
str;
$preg ="/<(a)>(.*?)<\/(\1)>/is";
$str = preg_replace($preg, "<a>\\2</a>", $a);
echo $str;
除此之外PHP還有一個 過濾標簽的函內數 你可以看容一下手冊
❸ php如何過濾編輯器的html標簽
選擇1.將特殊符號進行轉換,可以用htmlspecialchars把<變為「<」等
選擇2.用正則表達式替換,將標簽都刪除:
$content=preg_replace('/\<.+?\>/','',$content);
❹ 用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如何過濾空格
1、頭尾來過濾空格 trim
代碼:自
<html>
<body>
<?php
$str="HelloWorld!";
echo"Withouttrim:".$str;
echo"<br/>";
echo"Withtrim:".trim($str);
?>
<body>
<html>
輸出結果:
Withouttrim:HelloWorld!
Withtrim:HelloWorld!
2、內容中的空格
str_replace(" ","","$array");
str_replace(find,replace,string,count)
參數 描述
find 必需。規定要查找的值。
replace 必需。規定替換 find 中的值的值。
string 必需。規定被搜索的字元串。
count 可選。一個變數,對替換數進行計數。
❻ php怎樣用正則表達式提取span標簽中內容並過濾掉p和br標簽
你要過濾的字元串是不是就都是這種,就這么長的。
你的需求是不是就是把字元串裡面的內各種容標簽都去掉?
如果你的需求和上面的說的相符,不需要用正則表達式,PHP 提供了 strip_tags 函數,用來過濾字元串裡面的 html 標簽,接收兩個參數:第一個參數是要處理的字元串,第二個參數是允許(要保留)的tag
$str='<spanid="aaa"><p>11111</p><br><p>22222</p><span>';
echostrip_tags($str);//output:1111122222
echostrip_tags($str,'<span>');//output:<spanid="aaa">1111122222<span>
我覺得這可能是你的實際需求,如果不符合你的需求,繼續追問。