PHP – Find the last occurrence of a needle within a haystack using the iconv_strrpos() function

PHPServer Side ProgrammingProgramming

In PHP, the iconv_strrpos() function is used to findsthe last occurrence of a needle within a haystack. Or, we can say that the iconv_strrpos() function returns the last character number from the haystack.


string iconv_strrpos(string $haystack, str $needle, str $encoding)


iconv_strrpos() accepts three parameters: $haystack, $needle and $encoding.

  • $haystack− It denotes the whole string.

  • $needle− The $needle parameter is used to search the substring from the given whole string.

  • $encoding− if the $encoding parameter is absent or null, then the string will assume that it may be encoded in iconv.internal_encoding.

Return Values

iconv_strpos() returns the numeric position of the given first occurrence of the needle from the haystack. If the needle is not found, then the function will return False.

Note: From PHP 8.0 version, encoding is nullable and from PHP 7.1, iconv_strpos() function support for the negative offsets has been added.


 Live Demo

   # UTF-8 string
   $int = iconv_strrpos("hello world!","d", "UTF-8");
   // It will returns the number of character


Updated on 23-Aug-2021 08:22:54