如何获取字符串中最后一个字符的位置? [关闭]

For example, I have $string = "abc+def", how can i get position of 'f'? Is it possible of finding position of last character in the string, in the above example, 'f' is last character.

In PHP, as in most languages with zero-based indexing, the index of the last character in a string will be one less than the length of the string (strlen($string) - 1).

If you want the last character, just use strlen() the length - 1. Its starts at zero.

echo (strlen($string)-1); // 6
echo $string[strlen($string)-1]; // f

It has a total of 7 characters, but positions start at zero.

I use this code to find charater in string. I hope this helpful

$string = 'abc+def';
$stringcheck = 'f';
$stringSP = str_split($string);
$position = NULL;
foreach ($stringSP as $key => $value){
    if(strcasecmp($stringcheck,$value) == 0){
       $position = $key + 1;
    }
}