Php reg表达式以匹配任何html

Php reg表达式以匹配任何html,php,html,regex,Php,Html,Regex,我正在尝试编写PHP代码,找到HTML的一部分并用另一部分替换它。 我的起点和终点是和中的和。 在PHPcode找到它并替换为ckeditor表单文本区域中的HTML代码后,它再也找不到它了 $pattern = '/<!--top-index-start-->(.*?)<!--top-index-end-->/'; $replacement = '<!--top-index-start-->' . $_POST['editor1'] . '<!--

我正在尝试编写
PHP
代码,找到
HTML
的一部分并用另一部分替换它。
我的起点和终点是
中的

PHP
code找到它并替换为
ckeditor
表单文本区域中的
HTML
代码后,它再也找不到它了

$pattern = '/<!--top-index-start-->(.*?)<!--top-index-end-->/';  
$replacement = '<!--top-index-start-->' . $_POST['editor1'] . '<!--top-index-end-->';  
$indexcontent = preg_replace($pattern, $replacement, $indexcontent);  
$pattern='/(.*)/;
$replacement=''$_POST['editor1'].';
$indexcontent=preg_replace($pattern,$replacement,$indexcontent);

通过
它再也找不到它了
,你的意思是你找不到
之间的内容

当然,你已经完全更换了它。在我看来,你的抓捕小组倒过来了。试试这个:

$regex = "~(<!--top-index-start-->).*?(<!--top-index-end-->)~";
$replacement = "\1".$_POST['editor1']."\2";
$indexcontent = preg_replace($regex, $replacement, $indexcontent);
$regex=“~()*?()”;
$replacement=“\1”。$\u POST['editor1']。“\2”;
$indexcontent=preg_replace($regex,$replacement,$indexcontent);
解释


正则表达式中的捕获括号将顶部和结尾分隔符捕获到组1和组2。在替换字符串中,您将它们引用为
\1
\2
,以构建替换。

您尝试过任何东西吗?如果是,请让我们查看代码谢谢大家。这是一个修饰语的问题。我需要“s”修饰符来匹配所有内部。*?