简体   繁体   English

删除两个方括号之间的所有内容

[英]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. 是的,这是从phpbb发出的,我已经从数据库中拉出了它,没问题,但是我回显它时不希望它显示。

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); 编辑:我已经尝试过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); $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); 2. $output= preg_replace('#\\].*?\\[#', '', $entry);

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

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

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

You can use this regex to replace: 您可以使用此正则表达式替换:

$string = ' [attachment=0:randomID]<!-- ia0 -->randomIMGnam.png<!-- ia0 -->[/attachment:randomID]';
$string = preg_replace('/\[(.*?)\]/', '', $string);

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 


?>

Using regex might be heavy for this kind of task. 对于这种任务,使用正则表达式可能很繁重。
You could instead use a simple reasoning, whenever you meet a open bracket increment a counter by one, whenever you meet a close bracket decrement the counter by one. 取而代之,您可以使用简单的推理,只要遇到开括号将计数器加1,遇到碰括号将计数器减1即可。

And as long as your counter is > 0 just ignore the characters. 并且只要您的计数器> 0,就忽略字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM