❶ 請高手寫條preg_replace的PHP過濾
你的正則需要定界符,比如:
$s = preg_replace('/<p.*?>/', '', $s);
❷ php的preg_replace過濾emoji字元,要怎麼做
查找:<img[^>]*?alt="([^"]*?)">
替換為:$1
要學會舉一反三才行,對應改一下就好了。
❸ preg replace php中網址過濾
完全沒必要用正則
直接用內置函數strip_tags可以達到你的的需求
strip_tags(string,allow)
❹ php過慮html標簽的正則表達式preg_replace("/<.+>/t","",$str);
?是非貪婪匹配的標志,這么說吧
這里的<.+?>由於.是匹配非回車換行的所有字元,需要用?來限製成非貪婪的匹配,可以保證匹配到合適的就不再往後走了,也就是找到第一個>後就不再繼續了。
但如果不限制寫成<.+>,就是貪婪匹配,一組匹配能匹配多遠匹配多遠,比如<ooo>haha<abad>ee的時候就要一直往後找>,直到匹配到<ooo>haha<abad>,而這個肯定是你不想要的。
//-----
\t是製表符。但/<.+?>/t結尾那個不知道啥意思,樓下補充
❺ preg_replace 重復內容的替換次數問題
你這不是已經寫了函數嗎?
function tihuan($abb,$d){
return preg_replace("/1/",$d,$abb,1);
}
這就是函數啊.
你可以改一下,把要查找的字元串也寫成參數.
function tihuan($search,$to = '',$source_str,$times = 1){
if(empty($search)) return '';
if(empty($source_str)) return '';
return preg_replace($search,$to,$source_str,$times);
}
❻ php正則表達式preg_replace只替換一次(有很多匹配項……)
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
在 subject 中搜索 pattern 模式的匹配項並替換為 replacement。如果指定了 limit,則僅替換 limit 個匹配,如果省略 limit 或者其值為 -1,則所有的匹配項都會被替換。
如果要只一次,加上limit即可
<?php
$p = '/(123)(456)/';
$r = "結果:$1$2";
echo preg_replace($p,$r,"123456123456",1);
?>
這樣更易理解
<?php
$p = '/(123)(456)/';
$r = "結果:$1aaaaaa$2";
echo preg_replace($p,$r,"123456123456",1);
?>
❼ php preg_replace 正則替換
<?php
$str='sdfsdf<ahref="地址"target="_blank"><imgstyle="padding-bottom:數值px;width:數值px;height:數值px;"src="地址"/></a><img/>';
$str=preg_replace('/<ahref="[^"]+"s+target="_blank"><imgs+style="paddings*-s*bottom:[^"]+px;s*width:[^"]+px;s*height:[^"]+px;"s*src="[^"]+"/></a>/i','',$str);
echo$str;
?>
❽ 用preg_replace替換掉某字元串(正則)
new RegExp('【[^【]*(?=閱|評)+.*?】','g')
❾ preg_replace正則替換和str_replace有什麼區別
不知道你想問什麼 ,
preg_replace正則替換 這個是用正則來匹配 你需要的值的,然後把值取出來例子:
$a = preg_replace('正則','替換的內容',$b);//$b是賦值的對象
str_replace 是用來 替換 你已知的內容的
例子:
$a = '您好 aaaa';
$b = str_replace("aaaa","bbbb",$a);
echo $b;
最後結果為 "您好 bbbb"
總結:preg_replace是用來替換 類似性的有規律性的內容的,str_replace是用來 替換 已知的 內容的,可以是有規律的也可以是沒有規律的,替換的值 由你手動寫的
注:str_replace在php4里 無法使用
❿ PHP preg_replace 用法,打算將 含有 html tag過濾掉,例如
用string strip_tags ( string $str [, string $allowable_tags ] )
str The input string.
string allowable_tags 允許的標記
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>