Search code examples
phphtmlregexpreg-replacephpbb

remove everything between two square brackets


I've been trying to remove some text... between two sets of brackts... for two hours i have tried everything... i've been to existing questions here and the answers don't work for me... so here goes what i have

 [attachment=0:randomID]<!-- ia0 -->randomIMGnam.png<!-- ia0 -->[/attachment:randomID]

i really want to remove all of this from the beginning of a string i was able to remove everything inside the brackets but failed everytime to get rid of the image name

Yes this is from phpbb i've pulled it from my DB no problem but don't want it to be displayed when i echo it.

thanks in advance i really hope I really hope someone can help

edit: what i've tried 1. $extension_pos = strrpos($entry, '<!-- ia0 -->'); // find position of the last dot, so where the extension starts $output = substr($entry, 0, $extension_pos) . '' . substr($entry, $extension_pos);

2.$output= preg_replace('#\].*?\[#', '', $entry);

  1. $output = preg_replace('/\[[^]]*\]/', '', $entry);

  2. $output explode(']', $entry);

  3. $imagename = preg_replace('#([attachment.*?]).*?([/attachment.*?])#', '$1$2', $entry);


Solution

  • You could use regular expression as in example:

    <?php
        $string = 'test [attachment=0:randomID]randomIMGnam.png[/attachment:randomID] test2 [something]
        test3
    
        [/something] test4';
    
    
        echo preg_replace('#(\[(.*)\](.*)\[/.*\])#Us','',$string);
        // output test test2 test4 
    
    
    ?>