问题
I am trying to use preg_replace to remove any thing contained in a style tag. For example:
<img src="image.jpg" style="float:left;" />
Would be changed to:
<img src="image.jpg" />
Likewise:
<a href="link.html" style="color:#FF0000;" class="someclass">Link</a>
Would be changed to:
<a href="link.html" class="someclass">Link</a>
How would I write this regular expression?
preg_replace('EXPRESSION', '', $string);
回答1:
This should work:
preg_replace("@(<[^<>]+)\sstyle\=[\"\'][^\"\']+[\"\']([^<>]+>)@i", '$1$2', $string);
回答2:
I recommend using the right tool for the job and avoid using a regular expression.
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//*[@style]') as $node) {
$node->removeAttribute('style');
}
echo $dom->saveHTML();
Working Demo
If you must accomplish this job using a regular expression, the following will suffice.
$html = preg_replace('/<[^>]*\Kstyle="[^"]*"\s*/i', '', $html);
Explanation:
< # '<'
[^>]* # 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
回答3:
Look for style="..."
that is enclosed inside <
and >
and replace with matched group $1$2
(<.*)style="[^"]*"([^>]*>)
Online Demo
Here is working sample code
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;
?>
Output:
<img src="image.jpg" />
<a href="link.html" class="someclass">Link</a>
回答4:
This is the best one I could come up with
$re = "/\sstyle\=('|\").*?(?<!\\\\)\1/i";
$str = "<a href=\"link.html\" style=\"color:#FF0000;\"\" class=\"someclass\">Link</a>";
$subst = '';
$result = preg_replace($re, $subst, $str, 1);
outputs
<a href="link.html" class="someclass">Link</a>
demo:
http://regex101.com/r/uW2kB8/8
Explanation:
\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])
Which will even handle cases like this.
<a href="link.html" style="background-image:url(\"..\somimage.png\");" class="someclass">Link</a>
and
<a href="link.html" style="background-image:url('..\somimage.png');" class="someclass">Link</a>
and ( which it wont remove )
<a href="link.html" data-style="background-image:url('..\somimage.png');" class="someclass">Link</a>
and even
<a href='link.html' style='color:#FF0000;' class='someclass'>Link</a>
http://regex101.com/r/uW2kB8/11
unlike the other suggestions :)
来源:https://stackoverflow.com/questions/24641744/preg-replace-any-style-tags-expression