PHP preg_replace不起作用

时间:2015-12-14 15:24:09

标签: php html preg-replace

我使用此代码:

$message = preg_replace("/<div style=\'background-color:#C0C8D0;width:95%;\'>SMA Forr&aacute;sk&oacute;d: <a href=\'\' onclick=\'selectcode((.*));return false;\'>[ Mindet kijelol ]<\/a><\/div><div id=\'(.*)\' style=\'width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;\'><pre class=\'sma\' style=\'font-family:monospace;font-size: 12px;\'><ol><li style=\'font-weight: normal; vertical-align:top;\'><div style=\'font: normal normal 1em\/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\'>(.*)<\/div><\/li><\/ol><\/pre><\/div>/",'[sma]<pre>$3</pre>[/sma]',$message);

但它不起作用。原始的html:

<div style='background-color:#C0C8D0;width:95%;'>SMA Forr&aacute;sk&oacute;d: <a href='#' onclick='selectcode(16351);return false;'>[ Mindet kijelol ]</a></div><div id='16351' style="width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;"><pre class="sma" style="font-family:monospace;font-size: 12px;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">some-text</div></li></ol></pre></div>

2 个答案:

答案 0 :(得分:0)

你的html不匹配:

pattern: [..snip..] <a href=\'\' onclick
html   : [..snip..] <a href='#' onclick

所以......是的

答案 1 :(得分:0)

$re = "/<div style='background-color:#C0C8D0;width:95%;'>SMA Forr&aacute;sk&oacute;d: <a href='#' onclick='selectcode\\((.*)\\);return false;'>\\[ Mindet kijelol \\]<\\/a><\\/div><div id='(.*)' style=\"width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;\"><pre class=\"sma\" style=\"font-family:monospace;font-size: 12px;\"><ol><li style=\"font-weight: normal; vertical-align:top;\"><div style=\"font: normal normal 1em\\/1\\.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">(.*)<\\/div><\\/li><\\/ol><\\/pre><\\/div>/"; 
$str = "<div style='background-color:#C0C8D0;width:95%;'>SMA Forr&aacute;sk&oacute;d: <a href='#' onclick='selectcode(16351);return false;'>[ Mindet kijelol ]</a></div><div id='16351' style=\"width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;\"><pre class=\"sma\" style=\"font-family:monospace;font-size: 12px;\"><ol><li style=\"font-weight: normal; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">some-text</div></li></ol></pre></div>\n"; 

preg_match($re, $str, $matches);