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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

mb_strpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false

Parameters

ParameterDescription
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

Bytes vs charactersTry it yourself
<?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
Use with mb_substr()Try it yourself
<?php
$text = "Grüße aus München";
$pos = mb_strpos($text, "München");
echo mb_substr($text, $pos), "\n";

Output

München

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?