-4

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.

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
  • 1
    I didn't clearly understand your question but i think what you need is to count the number of your string in order to get the position of the last character. – Hope Sep 15 '14 at 00:53
  • what? you're looking the last position of `f` or position of the final string? – Kevin Sep 15 '14 at 00:55
  • possible duplicate of [Find last character in a string in PHP](http://stackoverflow.com/questions/4427172/find-last-character-in-a-string-in-php) – Halfwarr Sep 15 '14 at 01:17

3 Answers3

4

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).

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
Chris Nava
  • 6,614
  • 3
  • 25
  • 31
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.

Kevin
  • 41,694
  • 12
  • 53
  • 70
0

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;
    }
}