简体   繁体   中英

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM