【问题标题】:php preg_replace html contentphp preg_replace html 内容
【发布时间】:2011-07-04 13:19:53
【问题描述】:

我需要删除那些 html 块之间的内容:

$var1 ="

    <html><head>
    <meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'></head><body>
    <img alt='shopozilla' src='http://www.ssopte.com/images/2010/usdos-logo-seal.png' >

    <span style='font-family: Arial,Helvetica,sans-serif; color: rgb(93, 93, 93);
    font-size: 17px; font-weight: bold;'>shopozilla sent this message to
";

$var2 = "

    Section 222 of the sand sAct. Section 222(f) provides that the     records of the separtment of State and of diplomatic and consular  </font><br>
    </td></tr></tbody></table></td></tr></tbody></table></body></html> 

";

到目前为止我尝试过

<pre>
$content =  preg_replace("/$var1(.*)$var2/m", "", $htmlContent);
</pre>

但不工作,所以我需要一个应该工作的模式/正则表达式。

【问题讨论】:

标签: regex preg-replace html-parsing


【解决方案1】:

您的模式包含许多在正则表达式中具有特殊含义的字符,因此 preg_replace 对要搜索的内容感到困惑。只需使用str_replace,因为你真的不需要正则表达式。好久没做PHP了,试试看:

$pos1 = stripos($htmlcontent, $var1);
$pos2 = strripos($htmlcontent, $var2);

$content = substr_replace($htmlcontent, "", $pos1, $pos2 + strlen($var2));

还有one point that cannot be emphasized enough。

【讨论】:

    【解决方案2】:

    尝试去除preg外的图案。

    $pattern = "/$var1(.*)$var22/m";   //adding /s might help with the /m
    

    这样你就可以回显 $pattern;并检查它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多