简体   繁体   English

您如何检测特定单词是否在php中字符串的末尾

[英]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" 我正在尝试删除最后一个“ 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" 我想出了类似的方法,但是它会在检测到第一个“ ON”后删除所有内容,是否有办法向后运行或告诉您跳到最后一个“ ON”

You can use strrpos function to perform search from the end of the string: 您可以使用strrpos函数从字符串末尾执行搜索:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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