PHP mb_strpos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

Definition

In PHP, mb_strpos() finds where a piece of text first appears in a string and returns its character position.

Unlike strpos(), it counts characters, so accented letters do not shift the result.

Syntax

Call mb_strpos() with the values you need. Start with this line.

PHP
$pos = mb_strpos($haystack, $needle, 0, "UTF-8");
CallWhat it does
mb_strpos("Héllo", "l", 0, "UTF-8")Returns 2.
mb_strpos("Héllo", "z", 0, "UTF-8")Returns false.

The first position is 0. Always compare the result with === false.

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

Parameters

ParameterDescription
haystackRequired. The text to search in.
needleRequired. The text to look for.
offsetOptional. The character position to start from. Default is 0.
encodingOptional. The character encoding. Default is the internal encoding.

Return Value

Return ValueDescription
int|falseThe character position of the first match, or false if not found.

PHP Examples

Example 1 — Find a Letter After an Accent

Compare the character position with the byte position.

PHP
<?php
$text = "Héllo Wörld";

echo strpos($text, "ö"), PHP_EOL;
echo mb_strpos($text, "ö", 0, "UTF-8"), PHP_EOL;

How It Works

  1. Search for ö. It comes after the accented é.
  2. strpos() counts bytes. The é takes 2, so the result is 8.
  3. mb_strpos() counts characters. The result is 7.
  4. Use the multibyte version for text with accents.

Example 2 — Handle No Match

When the text is not found, the function returns false.

PHP
<?php
$pos = mb_strpos("Héllo", "z", 0, "UTF-8");

var_dump($pos);

if ($pos === false) {
    echo "Not found", PHP_EOL;
}

How It Works

  1. Search for a letter that is not there.
  2. The function returns false.
  3. Test with === false. Position 0 would otherwise look like a miss.
  4. Print a message when nothing is found.

Example 3 — Start from an Offset

Use the offset to skip the first match.

PHP
<?php
$text = "ababab";

echo mb_strpos($text, "b", 2, "UTF-8"), PHP_EOL;

How It Works

  1. The letter b is at positions 1, 3 and 5.
  2. Start at offset 2. The first b is skipped.
  3. The next match is at position 3.
  4. Use a loop with offsets to find every match.

Frequently Asked Questions

mb_strpos() returns the character position of the first match of a piece of text in a string.
mb_strpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false.
It returns false. Compare with === false.
strpos() counts bytes. mb_strpos() counts characters in the given encoding.
Yes. mb_stripos() ignores case. You can also lowercase both strings with mb_strtolower() first.

Next: mb_strtolower()

mb_strtolower() makes a string lowercase, including accented letters.

mb_strtolower() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful