Reputation: 21
Original code:
<div style="height:100px;" id="main" >
<a href="133"></a>
<blockquote color="123">
after replace
<div style="height:100px;" >
<a></a>
<blockquote>
i try the regex but its not work
preg_replace('#<(div|span|a|img|ul|li|blockquote).*( style=".*")?(.*)>#Us', '<$1$2>', $content);
anyone can help me to solve this problem? thank you!!
Upvotes: 1
Views: 222
Reputation: 3495
I do not have PHP available at this moment, so I'll write you a regex on Javascript, and you can port it easily. (I'll use the RegExp object so the regex will already be quoted for you)
'<div style="height:100px;" id="main" >'.replace(new RegExp('<([a-zA-Z0-9]*)(.*([ \t\r\n]style[ \t\r\n]*=[ \t\r\n]*(("[^"]*")|(\'[^\']*\'))))*[^>]*>'), '<$1$3>')
== <div style="height:100px;">
'<div style=\'height:100px;\' id="main" >'.replace(new RegExp('<([a-zA-Z0-9]*)(.*([ \t\r\n]style[ \t\r\n]*=[ \t\r\n]*(("[^"]*")|(\'[^\']*\'))))*[^>]*>'), '<$1$3>')
== <div style='height:100px;'>
'<div style="height:100px;">'.replace(new RegExp('<([a-zA-Z0-9]*)(.*([ \t\r\n]style[ \t\r\n]*=[ \t\r\n]*(("[^"]*")|(\'[^\']*\'))))*[^>]*>'), '<$1$3>')
== <div style="height:100px;">
'<div dfg dfg fdg>'.replace(new RegExp('<([a-zA-Z0-9]*)(.*([ \t\r\n]style[ \t\r\n]*=[ \t\r\n]*(("[^"]*")|(\'[^\']*\'))))*[^>]*>'), '<$1$3>')
== <div>
'<div>'.replace(new RegExp('<([a-zA-Z0-9]*)(.*([ \t\r\n]style[ \t\r\n]*=[ \t\r\n]*(("[^"]*")|(\'[^\']*\'))))*[^>]*>'), '<$1$3>')
== <div>
So its one regex which takes into account most possible situations.
Does this answer your question?
(Btw, you can replace those [ \t\r\n] with the whitespace shorthand if php's regex supports it and it works in multiline mode)
Upvotes: 0
Reputation:
Not recommending regex, but this probably works.
Edit: fixed option group, was in the wrong place.
Test case here: http://ideone.com/vRk1u
'~
( < (?:div|span|a|img|ul|li|blockquote) (?=\s) ) # 1
(?=
(?:
(?:[^>"\']|"[^"]*"|\'[^\']*\')*?
( # 2
\s style \s*=
(?: (?> \s* ([\'"]) \s* (?:(?!\g{-1}) .)* \s* \g{-1} ) #3
| (?> (?!\s*[\'"]) \s* [^\s>]* (?=\s|>) )
)
)
)?
)
\s* (?:".*?"|\'.*?\'|[^>]*?)+
( /?> ) # 4
~xs'
Upvotes: 1