The iconv_strrpos() function is an inbuilt function in PHP that searches for the last occurrence of a substring within a string while taking the character encoding of the string into account. It is similar to the strrpos() function, but it supports multi-byte character sets.
Syntax:
int|false iconv_strrpos(
string $haystack,
string $needle,
?string $encoding = null
)
Parameters: The function accepts three parameters that are described below:
- $haystack: This parameter is where we search for the last occurrence of a string.
- $needle: This parameter defines searching a sub-string in the $haystack parameter.
- $encoding: The character set to use. The default value is the internal encoding that will be used.
Return Values: The numeric position of the last occurrence of the substring within the string, or “false” if the substring is not found in the string.
Example 1: The following program demonstrates the iconv_strrpos() function.
PHP
<?php $str = "Hello world, hello universe!"; $pos = iconv_strrpos($str, "hello"); echo $pos; ?> |
Output:
13
Example 2: The following program demonstrates the iconv_strrpos() function.
PHP
<?php $str = "München ist schön. München ist groß."; $pos = iconv_strrpos($str, "München", "UTF-8"); echo $pos; ?> |
Output:
19
Reference: https://www.php.net/manual/en/function.iconv-strrpos.php
