0

I am having some troubles with removing empty [url=http://exp.com] [/url] tags from the string. Here is what I have, but it is incorrect bcz it removes also if there is something between those two tags.

$desc = preg_replace("#\[url=.*?\][(\W)]?\[/url\]#i", "", $desc);

1 Answer 1

1

try with this pattern:

$desc = preg_replace('~\[url=[^]]*]\s*+\[/url]~i', '', $desc);

The idea is to avoid the lazy quantifier using a character class that doesn't contain the closing square bracket (ie: [^]]).

The \s*+ allows only white characters between the opening and the closing tags, but you can remove it if you don't need it.

Note that a closing square bracket don't need to be escaped outside a character class and must be escaped inside a character class unless it is the first character. You can write [^]a] or [^a\]] or [^\]a] but not [^a]] that is interpreted as all characters but a followed by ]

I am surprise by what you are trying to do since [url=www.example.com][/url] stands for [url=www.example.com]www.example.com[/url]

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.