开发者

strrpos in PHP finding last character position instead?

开发者 https://www.devze.com 2023-03-31 02:14 出处:网络
$string = \"01234567890*****12345678901234567890*\"; echo strrpos($string,\'*****\'); why does this return 36 and not 15? (click here to test)
$string = "01234567890*****12345678901234567890*";  
echo strrpos($string,'*****');

why does this return 36 and not 15? (click here to test)

开发者_StackOverflow中文版from php.net

strrpos — Find the position of the last occurrence of a substring in a string

What am I missing out on???

Thank you!

SOLUTION: using the answers bellow, I provided a PHP4 alternative

$haystack = "01234567890*****12345678901234567890*";  
$needle              = '*****';

$position = strlen($haystack) - strlen($needle) - strpos(strrev($haystack),strrev($needle));
echo $position; // 11


Probably you're using PHP 4:

from php.net

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


WriteCodeOnline.com uses PHP 4.4.9 (test it with phpversion) and strrpos prior to 5.0 does only accept one character and not a string:

The needle can only be a single character in PHP 4.

That’s why you code is handled like strrpos($string,'*'). In PHP 5.0 and later the returned value would be 11.


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

PHP Version 5.3.5 result is 11

if you want to get last occurrence try this

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

精彩评论

暂无评论...
验证码 换一张
取 消