Tomasz
Tomasz

Reputation: 1368

Regex - remove four special characters between [image=][/image] BB tag

In a script I have a BB-image code that can have one of these two formats:

[image=http://i459.photobucket.com/albums/xxx/enkidu-wd/test/38602_450pom.jpg][/image] (without Alt text)

[image=https://m.photobucket.com/enkidu-wd/test/38602_450pomnik.jpg]alt text[/image] (with Alt text)

My goal is to always remove these four characters: $ & ' " - but ONLY IF these characters are added as the alt text (ie. between the [image=...]ALT TEXT[/image]). If these characters appear anywhere outside of the [image][/image] tags in the $text message, they should be ignored (not removed).

I tried to use preg_replace as below, but it doesn't work.. Note that $text is a message that may or may not include this [image][/image] tag.

$start = '\[image=';
$end  = '\[\/image\]';
$text = preg_replace(
    '#('.$start.')(.*)('.$end.')#i',
    '$1'.str_replace(array('$','&','\'','"'),'').(*).'$3',
    $text
);

Upvotes: 0

Views: 92

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can try something like this:

$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*\K[$&\'"]~i';

if other BBCODE tags can be included between image tags, you can change it like this:

$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*(?:\[(?!/image\b)[^][]*][^[$&\'"]*)*\K[$&\'"]~i';

use:

$result = preg_replace($pattern, '', $yourstring);

details:

~ # pattern delimiter
(?:
    \G(?!\A) # contiguous to the previous match, not at the start of the string
  | # OR
    [image\b[^]]*] # an opening image tag
)
[^[$&\'"]* #"# all that isn't an opening square bracket or one the chars
\K # remove all that has been matched before from the match result
[$&\'"]
~i

Explanation:

Since [^[$&\'"]* forbids an opening square bracket, once the closing [/image] is reached, the contiguity is broken and the \G anchor fails. The only way to continue is to find an other opening [image...] tag.

Upvotes: 2

Related Questions