PHP remove everything before last instance of a character

后端 未结 1 1156
耶瑟儿~
耶瑟儿~ 2021-02-08 08:02

Is there a way to remove everything before and including the last instance of a certain character?

I have multiple strings which contain >, e.g.

1条回答
  •  情话喂你
    2021-02-08 08:43

    You could use a regular expression...

    $str = preg_replace('/^.*>\s*/', '', $str);
    

    CodePad.

    ...or use explode()...

    $tokens = explode('>', $str);
    $str = trim(end($tokens));
    

    CodePad.

    ...or substr()...

    $str = trim(substr($str, strrpos($str, '>') + 1));
    

    CodePad.

    There are probably many other ways to do it. Keep in mind my examples trim the resulting string. You can always edit my example code if that is not a requirement.

    0 讨论(0)
提交回复
热议问题