PHP 8 reference · Multibyte String Functions
PHP mb_strpos() Function
Find the character position of the first occurrence of a substring in a multibyte string. Returns false if not found.
PHP 8 Since 4.0
Syntax
mb_strpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false
Parameters
| Parameter | Description |
|---|---|
haystack |
The string to search in. |
needle |
The string to find. |
offset optional |
The character position to start from. Default: 0 |
encoding optional |
The character encoding; defaults to mb_internal_encoding() (UTF-8). |
Return value
The character position, or false.
Examples
<?php
$text = "Grüße aus München";
echo strpos($text, "aus"), "\n"; // byte position
echo mb_strpos($text, "aus"), "\n"; // character position
Output
8 6
<?php
$text = "Grüße aus München";
$pos = mb_strpos($text, "München");
echo mb_substr($text, $pos), "\n";
Output
München
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
str.find()· Pythonstring.indexOf()· JavaScript
Related
strpos()· Find the position of the first occurrence of a substring. Returns false if it's not found, so compare with === false.mb_substr()· Return part of a string by character position, so multibyte characters are never cut in half. The UTF-8 version of substr().str_contains()· Check whether a string contains a substring and return true or false. Case-sensitive; added in PHP 8.0.- PHP Strings · tutorial