Reputation: 298
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);
Upvotes: 0
Views: 198
Reputation: 89557
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]
Upvotes: 1