Use this to get the image tags as well:
preg_match_all('~\[img\].*?\[/img\]~i', $body, $matches);
The i
modifier is for case insensitive matching.
If you don't want to get the image tags, use lookarounds:
preg_match_all('~(?<=\[img\]).*?(?=\[/img\])~i', $body, $matches);
By the way: [^(\[\/IMG\])]+
will match any character except (
, [
, /
, I
, M
, G
, ]
, )
(?<=\[img\])
is a positive lookbehind and makes sure your match has [img]
before it and
(?=\[/img\])
is a positive lookahead which makes sure your match has [/img]
after it. Those don't count as matches so they won't be returned in your capture group.