php - preg_replace with pattern and html tags not working -
i have this:
<span style="color:#ff8c00;"><strong>benjamin franklin</strong></span> or <span style="color:#ff8c00;"><strong><em>benjamin franklin</em></strong></span> or <span style="color:#ff8c00;">benjamin franklin</span> i need returns:
[color=#ff8c00]<strong>benjamin franklin</strong>[/color] [color=#ff8c00]<strong><em>benjamin franklin</em></strong>[/color] [color=#ff8c00]benjamin franklin[/color] i have pattern in php
~<span\s[^>]*\bstyle=[\'\"]color\:([\#a-za-z0-9\(\)\,]+)\;[\'\"]>([\<a-z0-9\>]+)(.+?)\s([\<\/a-z0-9\>]+)<\/span>~is but not works well, allthough in regexr show works http://regexr.com/3bjis
this returns me in php:
[/[color=#benjamin franklin php
$text = preg_replace( array( '~<span\s[^>]*\bstyle=[\'\"]color\:([\#a-za-z0-9\(\)\,]+)\;[\'\"]>([\<a-z0-9\>]+)(.+?)([\<\/a-z0-9\>]+)<\/span>~is', ), array( '[color=$1]$2$3$4[/color]', ), ltrim($text)); echo $text; regards.
edit: question different, pattern works in regexr link above posted, in php echoing returns incorrectly, not resort using xpath.
it wasn't easy may want.
'<span.+?style=(\'|\")color\:(.+?)\1[^>]*>(.+?)(?=\<\/span\>)<\/span>' and replacement
'[color=$2]$3[/color]' https://regex101.com/r/re1or3/4
to explain
\<spanmatch literal.+?match non-greedystyle=match literal(\'|\")match single or double quote, capture group 1 we'll use latter- `color: match literal
(.+?)match , capture non-greedy\1backrefrence quote style matched[^>]*match ( including empty ) not >>match literal(.+?)match , capture non-greedy(?=\<\/span\>)positive lookahead anchors non-greedy match,style=or\1bits makes sure matches first</span>encounters.<\/span>match literal
Comments
Post a Comment