【问题标题】:Extract the contents with the help of a regular expression借助正则表达式提取内容
【发布时间】:2017-06-26 01:29:15
【问题描述】:

有一个字符串的形式

'[Text1] text2 - text3' 或'text2 - text3' 也就是说,有时方括号中有 [text1],有时没有。并且总是在 text2 之前是 '-'

需要修剪的,php表示只保留'text2'

示例:

[蒂姆香肠] 对于阿努比斯 - 征服者 - 坚定的防守者

Overwrath - 评论头

Warpatch Hemos - 开始“发烧”

应该得到

For Anubis
Overwrath
Warpatch Hemos

【问题讨论】:

    标签: php regexp-replace


    【解决方案1】:

    尝试做这样的事情:

    $arr = array(
        '[Tim sausage] For Anubis - Conqueror - a staunch defender',
        'Overwrath - commented Head',
        'Warpatch Hemos - Began "fever"'
    );
    
    foreach($arr as $v)
    {
        $v = preg_replace("!\[.*\]!", '', $v);
        $v = preg_replace("!\-.*!", '', $v);
        echo $v.'<br/>;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 2010-11-14
      • 2015-04-28
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多