PHP removes everything until the last instance of a character

Is there a way to delete everything before and including the last instance of a specific character?

I have some lines that contain > , for example.

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

  • welcome > home

I need formatting strings so they become

  • mat

  • home

+8
php regex
source share
1 answer

You can use regex ...

 $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 this. Keep in mind that my examples crop the resulting string. You can always edit my sample code if this is not a requirement.

+22
source share

All Articles