Use preg_match_all to parse additional bbcode tags
P粉086993788
P粉086993788 2024-04-02 23:21:39
0
1
384

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粉086993788
P粉086993788

reply all(1)
P粉138711794

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);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!