Tcmxc
Tcmxc

Reputation: 491

How do you detect if a specific word is at the end of a string in php

I have event listings that look like this

PAPA ROACH AT THE PARAMOUNT IN HUNTINGTON ON APR 28, 2015

Im trying to remove everything after the last "ON"

$s="PAPA ROACH AT THE PARAMOUNT IN HUNTINGTON ON APR 28, 2015";
echo strstr($s, 'ON', true);

I came up with something like this but it removes everything after the first "ON" it detects, is there a way to run this backwards or tell to skip to the last "ON"

Upvotes: 0

Views: 52

Answers (1)

astax
astax

Reputation: 1767

You can use strrpos function to perform search from the end of the string:

$s="PAPA ROACH ON THE PARAMOUNT IN HUNTINGTON ON APR 28, 2015";
echo substr($s, 0, strrpos($s, ' ON ') + 3); // +3 is to include the word ON

Upvotes: 1

Related Questions