Php 如何定义正则表达式来删除两个字符之间的所有字符

Php 如何定义正则表达式来删除两个字符之间的所有字符,php,html,regex,string,replace,Php,Html,Regex,String,Replace,我对正则表达式模式的定义有问题。在下面的代码中,我想删除所有标记和之间的所有属性,如style=“bla bla”,但我想保留其他元素及其属性,如 编辑:我想得到这样的结果 <p style="text-align: center;">LOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUM</p> <p> <img class="alignnone" src="http://localhost/

我对正则表达式模式的定义有问题。在下面的代码中,我想删除所有
标记和
之间的所有属性,如
style=“bla bla”
,但我想保留其他元素及其属性,如

编辑:我想得到这样的结果

<p style="text-align: center;">LOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUM</p>
<p>
<img class="alignnone" src="http://localhost/themify/wp-content/uploads/2015/05/fortuna_novi-300x300.png" alt="fortuna_novi" width="112" height="112">
</p>
LOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUM

请参阅EJTH的评论,我不能经常重复:不要使用正则表达式进行DOM操作

在这种情况下,如果您只想删除所有
标记,可以使用

LOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUM
<img class="alignnone" src="http://localhost/themify/wp-content/uploads/2015/05/fortuna_novi-300x300.png" alt="fortuna_novi" width="112" height="112">

以下代码段可能对您有所帮助:

<p(?:\s+\w+(="[^"]*"|'[^']*'|\w+)?)*>|</p>
$re=“/(\\s*|)/”;
$str=“

LOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUM

\n\n\n

”; $subst=“$1”; $result=preg_replace($re,$subst,$str);

您应该添加预期的结果,这样问题就更清楚了。不要将正则表达式用于DOM操作。您只需使用DOM解析器就可以避免很多麻烦。@TingYiShih-现在我添加了结果
<p(?:\s+\w+(="[^"]*"|'[^']*'|\w+)?)*>|</p>
$re = "/(\\s*<p.*?\\s*>|<\\s*\\/p\\s*>)/"; 
$str = "<p style=\"text-align: center;\">LOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUMLOREM IPSUM</p>\n<p>\n<img class=\"alignnone\" src=\"http://localhost/themify/wp-content/uploads/2015/05/fortuna_novi-300x300.png\" alt=\"fortuna_novi\" width=\"112\" height=\"112\">\n</p>"; 
$subst = "$1"; 

$result = preg_replace($re, $subst, $str);