【问题标题】:Trim all characters before a certain substring (php)修剪某个子字符串之前的所有字符(php)
【发布时间】:2011-03-01 19:08:00
【问题描述】:

我有这个字符串:

"(data I don't care about) (invariable substring) (data I care about)"

知道不变的子串,如何修剪所有我不关心的数据?

【问题讨论】:

标签: php string substring


【解决方案1】:

这就足够了:

substr($string, (int)strpos("invariablestring", $string));

但会保持不变的字符串不变。如果要删除该字符串,只需将其长度添加到返回的值 strpos() 即可。

【讨论】:

    【解决方案2】:

    您可以获取偏移量,并且只从该偏移量中获取子字符串:

    if (($pos = strpos($str, $substr)) !== false) {
        $str = substr($str, $pos);
    }
    

    【讨论】:

      【解决方案3】:

      我发现的最简单的是:

      list(, $data_i_care_about) = explode('(invariable substring)', $all_the_data, 2)
      

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-23
        • 2022-11-01
        • 2013-02-07
        相关资源
        最近更新 更多