I made a custom function which acts like bbCode. I’m using preg_replace and regex. The only problem is that if I use more than one bbCode formatting, then just only one works..
[align=center][img]myimagelink[/img][/align]
If I enter this line, then the image appears BUT the [align=center]image[/align] also. How can I avoid this problem?
$patterns[2] = '@\[align=(.*)\](.*)\[\/align\]@si';
$patterns[9] = '@\[img\](.*\.jpg)\[\/img\]@si';
$replacements[2] = '<table align=\1><tr><td align=\1>\2</td></tr></table>';//ALIGN
$replacements[9] = '<img src=\"$1\"/>';//image
Changing the
.*expressions to non-greedy (.*?) will work for you.Example: