Heim >Backend-Entwicklung >PHP-Tutorial >请教一个正则问题
原字符串:
$arr1=array('#]+>([^\r\n#i', '#
]*>#i', '#?span>\r\n#i');
$arr2=array('$1 ', '
', '');
preg_replace($arr1, $arr2, $str);
\r\n是windows文本换行符,要注意对应文本的格式
没环境测试,自己摸索一下吧
$str1 = '
$s =<<< TXT<LI> <SPAN id=result_box lang=fr c="4" a="undefined" zd="null" closure_uid_235198886="43"> <SPAN class=hps zd="null" closure_uid_235198886="1391">la belle</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1392">courbe de</SPAN> <BR zd="null" closure_uid_235198886="1410"> <SPAN class=hps zd="null" closure_uid_235198886="1399">Parfait comme</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1400">cadeau pour votre amoureux</SPAN> <BR zd="null" closure_uid_235198886="1411"> <SPAN class=hps zd="null" closure_uid_235198886="1401">NOTE</SPAN> <SPAN zd="null" closure_uid_235198886="1402">: Laver</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1403">à la main</SPAN> </SPAN> <LI>TXT;echo strip_tags($s, '<li><br>');
$str1 = '<LI> <SPAN id=result_box lang=fr c="4" a="undefined" zd="null" closure_uid_235198886="43"> <SPAN class=hps zd="null" closure_uid_235198886="1391">la belle</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1392">courbe de</SPAN> <BR zd="null" closure_uid_235198886="1410"> <SPAN class=hps zd="null" closure_uid_235198886="1399">Parfait comme</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1400">cadeau pour votre amoureux</SPAN> <BR zd="null" closure_uid_235198886="1411"> <SPAN class=hps zd="null" closure_uid_235198886="1401">NOTE</SPAN> <SPAN zd="null" closure_uid_235198886="1402">: Laver</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1403">à la main</SPAN> </SPAN> <LI>';$reg = "/<span(.*)>|<\/span>|zd=\"(.*)\"|closure_uid_235198886=\"(.*)\"/iU";$str = preg_replace($reg, '', $str1);var_dump($str);exit;
$str1 = '<LI> <SPAN id=result_box lang=fr c="4" a="undefined" zd="null" closure_uid_235198886="43"> <SPAN class=hps zd="null" closure_uid_235198886="1391">la belle</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1392">courbe de</SPAN> <BR zd="null" closure_uid_235198886="1410"> <SPAN class=hps zd="null" closure_uid_235198886="1399">Parfait comme</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1400">cadeau pour votre amoureux</SPAN> <BR zd="null" closure_uid_235198886="1411"> <SPAN class=hps zd="null" closure_uid_235198886="1401">NOTE</SPAN> <SPAN zd="null" closure_uid_235198886="1402">: Laver</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1403">à la main</SPAN> </SPAN> <LI>';$reg = "/<span(.*)>|<\/span>|zd=\"(.*)\"|closure_uid_235198886=\"(.*)\"/iU";$str = preg_replace($reg, '', $str1);var_dump($str);exit;
$s =<<< TXT<LI> <SPAN id=result_box lang=fr c="4" a="undefined" zd="null" closure_uid_235198886="43"> <SPAN class=hps zd="null" closure_uid_235198886="1391">la belle</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1392">courbe de</SPAN> <BR zd="null" closure_uid_235198886="1410"> <SPAN class=hps zd="null" closure_uid_235198886="1399">Parfait comme</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1400">cadeau pour votre amoureux</SPAN> <BR zd="null" closure_uid_235198886="1411"> <SPAN class=hps zd="null" closure_uid_235198886="1401">NOTE</SPAN> <SPAN zd="null" closure_uid_235198886="1402">: Laver</SPAN> <SPAN class=hps zd="null" closure_uid_235198886="1403">à la main</SPAN> </SPAN> <LI>TXT;echo strip_tags($s, '<li><br>');
$patterns = array(
'/]*>/i',
'//i',
'/
]*>/i',
);
$replace = array(
'',
'',
'
',
);
echo preg_replace($patterns, $replace, $str);
这个不错。