Home  >  Q&A  >  body text

Use preg_match_all to parse additional bbcode tags

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

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

I want to remove everything in the string and use:

(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 deletes [attach][/attach], but does not delete [attach=full][/attach] =full exists in the message.

P粉086993788P粉086993788223 days ago436

reply all(1)I'll reply

  • P粉138711794

    P粉1387117942024-04-03 16:14:32

    Use preg_replace() instead of preg_match_all().

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

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

    reply
    0
  • Cancelreply