【问题标题】:remove everything between two square brackets删除两个方括号之间的所有内容
【发布时间】:2014-07-23 06:50:55
【问题描述】:

我一直在尝试删除一些文本...在两组括号之间...两个小时以来我已经尝试了所有内容...我一直在这里解决现有问题,但答案对我不起作用......所以这里 我有什么

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

我真的想从字符串的开头删除所有这些我能够删除括号内的所有内容但每次都无法删除图像名称

是的,这是来自 phpbb,我已经从我的数据库中提取了它,但不希望在我回显它时显示它。

提前谢谢我真的希望我真的希望有人能提供帮助

编辑:我尝试过的 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);

【问题讨论】:

  • 发布您尝试过的内容..
  • 您可以使用REGEX 进行此操作
  • @Luke 预期的输出是什么?

标签: php html regex preg-replace phpbb


【解决方案1】:

您可以使用这个正则表达式来替换:

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

【讨论】:

  • 警告:preg_replace():未知修饰符“g”
  • @LukeVyner:- 您可以删除 g,因为 g 隐含在 preg_replace() 中,您不需要包含它
【解决方案2】:

您可以使用正则表达式,例如:

<?php
    $string = 'test [attachment=0:randomID]randomIMGnam.png[/attachment:randomID] test2 [something]
    test3

    [/something] test4';


    echo preg_replace('#(\[(.*)\](.*)\[/.*\])#Us','',$string);
    // output test test2 test4 


?>

【讨论】:

    【解决方案3】:

    对于此类任务,使用正则表达式可能会很繁重。
    您可以改为使用简单的推理,每当遇到左括号时,计数器加一,遇到右括号时,计数器减一。

    只要你的计数器大于 0,就忽略这些字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多