Reputation: 1
Trying to strip some BBCode from some text. I would like to remove everything between a [img] and a [/img], using a PHP preg_replace function, for example:
Here is my image[img]http://www.abc.com/image1.jpg[/img] and more text
Match: [img] followed by any number of characters followed by [/img]
Result:
Here is my image and more text
Thanks.
Upvotes: 0
Views: 137
Reputation: 1040
don't forget to group your content e.g. '/[img](.+)[\/img]/i', so in you replace condition you can reference the value between the tags '<img src="$1" />'
Upvotes: 0
Reputation: 35927
First, find the pattern that would match your BBCode tag:
\[img\][^\[]+\[/img\]
The only hard part is the class [^\]]
. The \[
means any opening bracket and the ^ means NOT. So this class will match everything that is not a [
.
You could also replace the class with .+
and use the U (ungreedy) option.
Now that you now which pattern to use, you just have to replace it with... an empty string. And the job is done!
This is a very basic regexp, it's important that you understand it and that you are able to reproduce it
Upvotes: 2
Reputation: 8254
/\[img\].*?\[\/img\]/i
will take care of everything between [img]
and [/img]
(case in-sensitive)
Upvotes: 1