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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.