Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

preg_match_all for atttach bbcode

I have two types of bbcode:
[attach]1234[/attach]
[attach=full]1234[/attach]

$message = 'this is message with attach [attach=full]1234[/attach]

I want to remove everything from string and using:

(preg_match_all('/\[ATTACH((.*?)\](.+?)\[\/ATTACH\]/i', $message, $out, PREG_SET_ORDER))
if (preg_match_all('/\[ATTACH((.*?)\](.+?)\[\/ATTACH\]/i', $message, $out, PREG_SET_ORDER))
{   
    for ($i=0;$i<count($out);$i++)
    {
        $replace_src[] = $out[$i][0];
        $replace_str[] = $out[$i][1];
        $newMessage = str_ireplace($replace_src, $replace_str, $message);
    }
}

This code remove the [attach][/attach] but not remove the [attach=full][/attach]
=full exsist in message.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use preg_replace(), not preg_match_all().

Use an optional group to match the optional =xxx after attach.

$newMessage = preg_replace('/\[ATTACH(?:=.*?)?\](.+?)\[\/ATTACH\]/i', '$1', $message);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading