PHP删除字符最后一个实例之前的所有内容

PHP删除字符最后一个实例之前的所有内容,php,regex,Php,Regex,有没有办法删除某个角色之前的所有内容,包括最后一个实例 我有多个字符串,其中包含,例如 the>cat>sat>on>the>mat welcome>home 我需要对字符串进行格式化,使其成为 mat home 您可以使用正则表达式 $str = preg_replace('/^.*>\s*/', '', $str); …或使用explode() $tokens = explode('>', $str); $str = trim(end($tokens)); $str = t

有没有办法删除某个角色之前的所有内容,包括最后一个实例

我有多个字符串,其中包含
,例如

  • the>cat>sat>on>the>mat

  • welcome>home

  • 我需要对字符串进行格式化,使其成为

  • mat

  • home


  • 您可以使用正则表达式

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

    …或使用
    explode()

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

    …或
    substr()

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


    可能还有很多其他的方法。请记住我的示例修剪生成的字符串。如果不需要,您可以随时编辑我的示例代码。

    对于上述建议,哪种可能的重复操作是最便宜的操作?@Julian Profile,请参阅!我怀疑最后一个可能是最快的。