Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php正则表达式删除mso标记_Php_Regex_Preg Replace - Fatal编程技术网

php正则表达式删除mso标记

php正则表达式删除mso标记,php,regex,preg-replace,Php,Regex,Preg Replace,我有以下html代码: $html = "<P style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; padding: 4px;" class=MsoNormal>text</P>"; $html=“text”; 我需要删除所有mso-*标记,结果将是: $html = "<P style="padding: 4px;" class=MsoNormal>text</P>";

我有以下html代码:

$html = "<P style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; padding: 4px;" class=MsoNormal>text</P>";
$html=“

text

”;
我需要删除所有mso-*标记,结果将是:

$html = "<P style="padding: 4px;" class=MsoNormal>text</P>";
$html=“

text

”;
如何使用php?? 非常感谢

这会有用的:

echo preg_replace(
    '(
        mso-   # match anything with the mso vendor prefix
        .+?    # followed by at least one character
        ;      # up to the first semicolon
        [ ]*   # and an optional space
    )xi',
    '',        // replace that match with nothing
    $html
);

但是,如果
$html
中不止一行html,那么请看一看,了解如何轻松可靠地从html中的元素获取属性。然后使用上面的正则表达式替换节点值。

您也可以试试这个

(mso-[^:]*:[^;]*;)
然而,别忘了不要用正则表达式解析html,这真是一个大罪


代码:

$html = "<p style='mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; padding: 4px;' class=MsoNormal>text</P>";

$cleanHtml = preg_replace('(mso-[a-z\-: ]+; )i', '', $html);

echo $cleanHtml;
<P style='padding: 4px;' class=MsoNormal>text</P>
$html=“

text

”; $cleanHtml=preg_replace(‘(mso-[a-z\-:]+;)i’,‘,$html); echo$cleanHtml;
输出:

$html = "<p style='mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; padding: 4px;' class=MsoNormal>text</P>";

$cleanHtml = preg_replace('(mso-[a-z\-: ]+; )i', '', $html);

echo $cleanHtml;
<P style='padding: 4px;' class=MsoNormal>text</P>

text


我已经测试了Kameleon博士的解决方案:它工作正常,但并不适用于所有情况。例如,对于以下代码,mso-*属性将不会删除:

<p style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto' class=MsoNormal>text</P>

最好的方面

可能重复的@Pekka在我看来不是一个好的dup。它基本上只是说使用HTMLPurifier或Tidy,总共只有一个答案。@Gordon我想这取决于OP真正想要什么。如果他想清理所有微软的东西,HTMLPurifier确实是我所知道的最好的方法。如果他想完全按照上面所说的去做,那就不一样了。@Ste你能澄清一下你的目的是什么吗:清理这个特定的代码片段还是清理所有微软的东西。这是一个有效的正则表达式解决方案。虽然我会使用
~
/
作为正则表达式分隔符,并将
-
放在字符类的末尾,以避免过度跳过:
preg\u replace('~mso-[a-z:-]+;~I','','$html)
$cleanHtml = preg_replace('(mso-[a-z0-9\s\-:;]+)i', '', $html);