Strrpos in PHP finding the last character position instead?

$string = "01234567890*****12345678901234567890*"; echo strrpos($string,'*****'); 

why does this return 36, not 15? ( click here to check)

from php.net
strrpos . Find the position of the last occurrence of the substring. in line

What am I missing ???? Thanks!

SOLUTION: using the answers below, I have provided an alternative to PHP4

 $haystack = "01234567890*****12345678901234567890*"; $needle = '*****'; $position = strlen($haystack) - strlen($needle) - strpos(strrev($haystack),strrev($needle)); echo $position; // 11 
+6
source share
3 answers

Maybe you are using PHP 4:

from php.net

needles : If the needle is not a string, it is converted to an integer and applied as the ordinal value of the character. A needle can be only one character in PHP 4.

+10
source

WriteCodeOnline.com uses PHP 4.4.9 (test it with phpversion ) and strrpos up to 5.0 accepts only one character, not a string:

A needle can be only one character in PHP 4.

That is why you code is processed as strrpos($string,'*') . In PHP 5.0 and later, the return value will be 11.

+6
source
 $string = "01234567890*****12345678901234567890*"; echo strrpos($string,'*****'); 

The result of PHP version 5.3.5 is 11

if you want the last occurrence try this

 $string = "01234567890*****12345678901234567890*"; echo strripos($string,'*');//36 
+2
source

Source: https://habr.com/ru/post/896073/


All Articles