Reputation: 23959
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.
the > cat > sat > on > the > mat
welcome > home
I need the strings to be formatted so they become
mat
home
Upvotes: 15
Views: 13360
Reputation: 490153
You could use a regular expression...
$str = preg_replace('/^.*>\s*/', '', $str);
...or use explode()
...
$tokens = explode('>', $str);
$str = trim(end($tokens));
...or substr()
...
$str = trim(substr($str, strrpos($str, '>') + 1));
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.
Upvotes: 35