Jump to content

Search and replace but only if another string is present


Phalynx

Recommended Posts

What is the correct code to accomplish a search & replace with specific parameters?

 

I have for example this:

[TD=class: myclass]Dynamic String, can contain all chars, simple?

[TD=class: myclass]Another Dynamic String, yup!

 

I want this:

[TD="class: myclass"]Dynamic String, can contain all chars, simple?

[TD="class: myclass"]Another Dynamic String, yup!

 

 

So just remove the URL tag BUT only if prefixed with the [td BBCode

 

I tried already these pattern with preg_match_all, but failed miserably:

'/\[TD="class: myclass"\]\[url="http:\/\/my.com\/static/<.*>/'

 

Does anyone has a solution? Thanks in advance...

You can include the TD in the search text so long as you remember to put it back when you're done.

//             v------1------v               v-2-v
preg_replace('/(\[TD=[^\]]+\])\[uR L=[^\]]+\](.*?)\[\/URL\]/i', '$1$2', $string)

(Don't forget to remove the space from the "UR L")

BBCode shoudn't be parsed in code/php tags

 

Many people like to use [url=http://google.com]this[/url]

It doesn't get translated to HTML, but the forum does add a http:// prefix. After copy/pasting what I typed and removing the space,

preg_replace('/(\[TD=[^\]]+\])\[url=http://[^\]]+\](.*?)\[\/URL\]/i', '$1$2', $string)

Now as it turns out, that's not actually harmful in this case, but still...

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.