Lovelock
Lovelock

Reputation: 8105

Replace [url=URL] regex

looking for a way to remove any text that is

[url=]

That would be easy, but the url itself is a URL I dont have control of.

e.g or urls:

[url=wqffqwfq] [url=qwfwqfqf]

etc.

Looking for a preg replace, currently have this:

$post_text = preg_replace('%\[URL[^\]]*\][^\[\]]*|\[/URL[^\]]*\]%i', '', $post_text);

But that removes the image that comes after the url.

Thanks.

Upvotes: 0

Views: 63

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477686

I think you use too much escape characters. You can however use lazy capture groups:

$string = '[foo] buz [url=wqffqwfq] qux [bar] foobar [url=qwfwqfqf] faq';
$pattern = '/\[url=(.*?)\]/i';
$replace = '';
$string = preg_replace($pattern,$replace,$string);
//$string = '[foo] buz  qux [bar] foobar  faq';

If the assignment (=) character is optional, you can use the /\[url(.*?)\]/i pattern.

Information: .*? means a non-greedy capture: this means that . is captures, as long as there is no way to escape the group. From the moment it is possible, the regex mechanism escapes the group.

Upvotes: 1

Related Questions