Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – mb_strrpos() function
The mb_strrpos() function in PHP is used to find the position of the last occurrence of a string in another string. This function performs the multibyte safe strrpos() operation based on the number of characters and is particularly useful when working with multibyte character encodings like UTF-8.
Syntax
int mb_strrpos( string $haystack, string $needle, int $offset = 0, string $encoding = null )
Parameters
mb_strrpos() accepts the following four parameters −
$haystack − The string to search in.
$needle − The string to search for in the haystack.
$offset − Search offset. If negative, the search starts from this many characters from the end of the string.
$encoding − Character encoding to use. If omitted, the internal character encoding is used.
Return Value
mb_strrpos() returns the numeric position of the last occurrence of the needle in the haystack string. If the needle is not found, it returns false.
Example 1
Basic usage to find the last occurrence of a substring −
<?php
mb_internal_encoding("UTF-8");
$text = "Hello World Hello";
$position = mb_strrpos($text, "Hello");
var_dump($position);
?>
int(12)
Example 2
Searching with multibyte characters −
<?php
mb_internal_encoding("UTF-8");
$text = "????????????";
$position = mb_strrpos($text, "?????");
var_dump($position);
?>
int(6)
Example 3
Using offset parameter −
<?php
mb_internal_encoding("UTF-8");
$text = "Hello World Hello";
$position = mb_strrpos($text, "Hello", -10);
var_dump($position);
?>
int(0)
Conclusion
The mb_strrpos() function is essential for finding the last occurrence of substrings in multibyte strings. It properly handles Unicode characters and provides reliable string searching capabilities for international applications.
