PHP - Return everything after delimiter

后端 未结 7 1635
北恋
北恋 2021-01-19 01:00

There are similar questions in SO, but I couldn\'t find any exactly like this. I need to remove everything up to (and including) a particular delimiter. For example, given t

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 01:21

    Use this preg_replace call:

    $str = 'File:MyFile.jpg';
    $repl = preg_replace('/^[^:]*:/', '', $str); // MyFile.jpg
    

    OR else avoid regex and use explode like this:

    $repl = explode(':', $str)[1]; // MyFile.jpg
    

    EDIT: Use this way to avoid regex (if there can be more than one : in string):

    $arr = explode(':', 'File:MyFile.jpg:foo:bar');
    unset($arr[0]);
    $repl = implode(':', $arr); // MyFile.jpg:foo:bar
    

提交回复
热议问题