Preg_replace任何样式标签表达式


preg_replace any style tags expression

我试图使用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);

我建议使用正确的工具,避免使用正则表达式。

$dom = new DOMDocument;  
$dom->loadHTML($html); 
$xpath = new DOMXPath($dom);  
foreach ($xpath->query('//*[@style]') as $node) {
    $node->removeAttribute('style'); 
}
echo $dom->saveHTML(); 
演示工作

如果必须使用正则表达式完成此工作,则以下命令就足够了。

$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)
演示工作

应该可以:

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

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

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

在线演示

下面是工作示例代码

示例代码:

<?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>

这是我能想到的最好的一个

$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

不像其他的建议:)