denhamd2 Posted December 17, 2009 Share Posted December 17, 2009 Hi, I'm trying to replace all instances of a HTML <span> tag with the class="eventname" with <strong> tags instead. Currently: <span class="eventname">Text here</span> Desired: <strong>Text here</strong> Any ideas how I'd do this using the preg match function in PHP? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/ Share on other sites More sharing options...
rajivgonsalves Posted December 17, 2009 Share Posted December 17, 2009 something like this <?php $str = '<span class="eventname" id="test">Text here</span><br /> <span>test one</span>'; echo preg_replace('#<span[^>]*class="eventname"[^>]*>(.*?)</span>#', "<b>$1</b>", $str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979190 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 Personally I'd go with $output = preg_replace('%<span[^>]+\bclass="eventname"[^>]*>(.*?)</span>%s', '<strong>$1</strong>', $input); Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979407 Share on other sites More sharing options...
thebadbad Posted December 17, 2009 Share Posted December 17, 2009 Or preg_replace('~<span\b[^>]+\bclass\s?=\s?([\'"])eventname\1[^>]*>(.*?)</span>~is', '<strong>$2</strong>', $input) Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979504 Share on other sites More sharing options...
salathe Posted December 17, 2009 Share Posted December 17, 2009 Or echo preg_replace( '@(?P<mouse> <:3_}~~~ </mouse>P?)'. '?\074(?P<tag>SP\x{61}(?x-i: n\b))[^>]+'. '\b[Clsa]{5}(?<=(?=PAN*)[\w\h]{9})\s?'. '=\s?([\047\042])eVeNtNaMe\g{-1}[^>'. ']*>(?#\K)((?s:.*?))</(?P=tag)>@i', '<strong>$4</strong>', $str ); Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979540 Share on other sites More sharing options...
thebadbad Posted December 17, 2009 Share Posted December 17, 2009 That's so straight forward, why didn't I think of that? Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979555 Share on other sites More sharing options...
nrg_alpha Posted December 18, 2009 Share Posted December 18, 2009 Or echo preg_replace( '@(?P<mouse> <:3_}~~~ </mouse>P?)'. '?\074(?P<tag>SP\x{61}(?x-i: n\b))[^>]+'. '\b[Clsa]{5}(?<=(?=PAN*)[\w\h]{9})\s?'. '=\s?([\047\042])eVeNtNaMe\g{-1}[^>'. ']*>(?#\K)((?s:.*?))</(?P=tag)>@i', '<strong>$4</strong>', $str ); And here salathe basically questions the readability of my patterns?! lol Salathe, put down the eggnog, you've had enough for one evening! Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979668 Share on other sites More sharing options...
cags Posted December 18, 2009 Share Posted December 18, 2009 [ot] Or echo preg_replace( '@(?P<mouse> <:3_}~~~ </mouse>P?)'. '?\074(?P<tag>SP\x{61}(?x-i: n\b))[^>]+'. '\b[Clsa]{5}(?<=(?=PAN*)[\w\h]{9})\s?'. '=\s?([\047\042])eVeNtNaMe\g{-1}[^>'. ']*>(?#\K)((?s:.*?))</(?P=tag)>@i', '<strong>$4</strong>', $str ); I hope your happy salathe, I thought I was beginning to get to grips with Regex, after seeing that pattern I have plummeted into a bout of depression.[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-979740 Share on other sites More sharing options...
salathe Posted December 19, 2009 Share Posted December 19, 2009 [ot] I hope your happy salathe, I thought I was beginning to get to grips with Regex, after seeing that pattern I have plummeted into a bout of depression. Sorry! To be fair, it's really not all that complicated and certainly not the craziest regex I've ever written. The pattern is the same as presented previously in the thread only obfuscated with useless parts (like the mouse) or alternative representations (like the hex escape sequences, named back references). If there's any part you'd like clarification on, I'm only a PM (or thread) away. [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/#findComment-980335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.