$arr1 = array ("llo" "world", "ef", "gh" );
检查 $str1
是否以 $arr1
中的某个字符串结尾的最佳方法是什么?
正确/错误的答案很好,尽管知道 $arr1 元素的数量作为答案(如果为真)会很好。
例子:
$pos= check_end("world hello");//$pos=0; because ends with llo
$pos= check_end("hello world");//$pos=1; because ends with world.
除了在 for 语句中将 $arr1
的所有元素与 $str1
的结尾进行比较之外,还有什么更好/更快/特别的方法吗?
最佳答案
在我的头顶上......
function check_end($str, $ends)
{
foreach ($ends as $try) {
if (substr($str, -1*strlen($try))===$try) return $try;
}
return false;
}
关于php:检查字符串是否以数组中的元素结尾的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10296715/