Jump to content

Problem with string pattern replacement


denhamd2

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/185469-problem-with-string-pattern-replacement/
Share on other sites

Or :shy:

 

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
);

Or :shy:

 

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!  :tease-03:

[ot]

Or :shy:

 

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]

[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]

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.