0

我正在研究 php-html-js 模板外壳,我想通过 ajax 回调类,作为模板 html 的一部分。

html的视图是这样的:

<center>this is main part</center>
<button class="btns"> class buttons </button>
<div> also main </div>
<!--[part:AJAX]-->
<div> BUT This part at first should be removed with comments, and all inside <!--[part:ANYVAR]--> till
the same comment: </div>
<!--[part:AJAX]-->

但是在我需要做相反的事情之后:删除除内部之外的所有内容

PS谢谢你的关注:)

4

1 回答 1

1

这个正则表达式将找到注释和里面的内容:

$pattern = "/(<!--\[part:AJAX\]-->.*<!--\[part:AJAX\]-->)/ms";
preg_match($pattern, $html, $matches);
echo $matches[1];

要在评论之间删除,请将您的模式更改为:

$pattern = "/(<!--\[part:AJAX\]-->(.*)<!--\[part:AJAX\]-->)/ms";

然后

echo str_replace($matches[2], "", $html);
于 2012-07-27T12:32:43.207 回答