preg_replace任何样式标签表达式

时间:2014-07-08 21:16:12

标签: php regex preg-replace

我正在尝试使用preg_replace来删除样式标记中包含的任何内容。例如:

<img src="image.jpg" style="float:left;" />

将更改为:

<img src="image.jpg" />

同样地:

<a href="link.html" style="color:#FF0000;" class="someclass">Link</a>

将更改为:

<a href="link.html" class="someclass">Link</a>

我该怎么写这个正则表达式?

preg_replace('EXPRESSION', '', $string);

4 个答案:

答案 0 :(得分:3)

我建议使用正确的tool作为作业,并避免使用正则表达式。

$dom = new DOMDocument;  
$dom->loadHTML($html); 

$xpath = new DOMXPath($dom);  

foreach ($xpath->query('//*[@style]') as $node) {
    $node->removeAttribute('style'); 
}

echo $dom->saveHTML(); 

Working Demo

如果您必须使用正则表达式完成此作业,则以下内容就足够了。

$html = preg_replace('/<[^>]*\Kstyle="[^"]*"\s*/i', '', $html);

<强>解释

<           # '<'
[^>]*       # any character except: '>' (0 or more times)
 \K         #  resets the starting point of the reported match
 style="    #  'style="'
  [^"]*     #    any character except: '"' (0 or more times)
  "         #    '"'
\s*         # whitespace (\n, \r, \t, \f, and " ") (0 or more times)

Working Demo

答案 1 :(得分:1)

这应该有效:

preg_replace("@(<[^<>]+)\sstyle\=[\"\'][^\"\']+[\"\']([^<>]+>)@i", '$1$2', $string);

答案 2 :(得分:1)

查找style="..."<中包含的>并替换为匹配的群组$1$2

(<.*)style="[^"]*"([^>]*>)

Online Demo


这是working sample code

示例代码:

<?php
    $re = "/(<.*)style=\"[^\"]*\"([^>]*>)/";
    $str = "<img src=\"image.jpg\" style=\"float:left;\" />\n\n<a href=\"link.html\" style=\"color:#FF0000;\" class=\"someclass\">Link</a>";
    $subst = '$1$2';

    $result = preg_replace($re, $subst, $str);
    print $result;
?>

输出:

<img src="image.jpg"  />

<a href="link.html"  class="someclass">Link</a>

答案 3 :(得分:0)

这是我能想到的最好的

$re = "/\sstyle\=('|\").*?(?<!\\\\)\1/i";
$str = "<a href=\"link.html\" style=\"color:#FF0000;\"\" class=\"someclass\">Link</a>";
$subst = '';

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

输出

<a href="link.html" class="someclass">Link</a>

演示:

http://regex101.com/r/uW2kB8/8

说明:

   \s match any white space character [\r\n\t\f ]
style matches the characters style literally (case insensitive)
\= matches the character = literally
1st Capturing group ('|")
    1st Alternative: '
        ' matches the character ' literally
    2nd Alternative: "
        " matches the character " literally
.*? matches any character (except newline)
    Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
(?<!\\) Negative Lookbehind - Assert that it is impossible to match the regex below
    \\ matches the character \ literally
\1 matches the same text as most recently matched by the 1st capturing group
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

甚至会处理这样的案件。

<a href="link.html" style="background-image:url(\"..\somimage.png\");" class="someclass">Link</a>

<a href="link.html" style="background-image:url('..\somimage.png');" class="someclass">Link</a>

和(它不会删除)

<a href="link.html" data-style="background-image:url('..\somimage.png');" class="someclass">Link</a>

甚至

<a href='link.html' style='color:#FF0000;' class='someclass'>Link</a>

http://regex101.com/r/uW2kB8/11

与其他建议不同:)