0

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.

3
  • 2
    Did you try this regex "/[url(.*?)]/i"? Commented Dec 27, 2014 at 23:45
  • You can also try "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?).*$)@";. Commented Dec 27, 2014 at 23:47
  • Can you give us an example with the worst situation, meanwhile I think you should try what @charlotte Dunois said. Commented Dec 27, 2014 at 23:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.