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

Dar*_*ney 9 php regex

有没有办法在包含特定字符的最后一个实例之前删除所有内容?

我有多个包含的字符串>,例如

  1. the > cat > sat > on > the > mat

  2. welcome > home

我需要格式化字符串,以便它们成为

  1. mat

  2. home

ale*_*lex 26

你可以使用正则表达式......

$str = preg_replace('/^.*>\s*/', '', $str);
Run Code Online (Sandbox Code Playgroud)

CodePad.

......或者使用explode()......

$tokens = explode('>', $str);
$str = trim(end($tokens));
Run Code Online (Sandbox Code Playgroud)

CodePad.

......或substr()......

$str = trim(substr($str, strrpos($str, '>') + 1));
Run Code Online (Sandbox Code Playgroud)

CodePad.

可能还有很多其他方法可以做到这一点.请记住我的示例修剪结果字符串.如果不是必需的话,您可以随时编辑我的示例代码.