代码之家  ›  专栏  ›  技术社区  ›  Alex

从字符串中获取字符串后面的字符串

  •  43
  • Alex  · 技术社区  · 15 年前

    最快的方法是什么 重要的东西 从这样的字符串中分离:

    bla-bla_delimiter_important_stuff
    

    _delimiter_ 总是存在的,但是字符串的其余部分可以更改。

    5 回复  |  直到 8 年前
        1
  •  63
  •   jon_darkstar    15 年前

    在这里:

    $arr = explode('delimeter', $initialString);
    $important = $arr[1];
    
        2
  •  32
  •   zemagel    15 年前
    $result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));
    
        3
  •  6
  •   Hamish    15 年前
    $importantstsuff=数组弹出(分解(''u delimiter',$string));
    
        4
  •  5
  •   Byron Whitlock    15 年前
    $string = "bla-bla_delimiter_important_stuff";
    list($junk,$important_stufF) = explode("_delimiter_",$string);
    
    echo $important_stuff;
    > important_stuff
    
        5
  •  5
  •   Yair Budic    12 年前

    我喜欢这种方法:

    $str="bla-bla_delimiter_important_stuff";
    $del="_delimiter_";
    $pos=strpos($str, $del);
    

    从分隔符结尾剪切到字符串结尾

    $important=substr($str, $pos+strlen($del)-1, strlen($str)-1);
    

    注:

    1) 对于substr,字符串从“0”开始,而对于strpos&strlen,则取字符串的大小(从“1”开始)

    2) 使用一个字符分隔符也许是个好主意

    推荐文章