Jump to content

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

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

?>

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]

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.