PHP iconv_strpos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_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 iconv_strpos() with the values you need. Start with this line.

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

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

Library declarations
PHP
iconv_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 set. Default is the iconv 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 iconv_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. iconv_strpos() counts characters. The result is 7.
  4. Use the character version for text with accents.

Example 2 — Handle No Match

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

PHP
<?php
var_dump(iconv_strpos("Héllo", "z", 0, "UTF-8"));

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 with var_dump() to see the type.

Example 3 — Start from an Offset

Use the offset to skip the first match.

PHP
<?php
echo iconv_strpos("ababab", "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

iconv_strpos() returns the character position of the first match of a piece of text in a string.
iconv_strpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false.
It returns false. Compare with === false.
strpos() counts bytes. iconv_strpos() counts characters in the given encoding.
Yes. mb_strpos() does the same job and is built into the mbstring extension.

Next: iconv_strrpos()

iconv_strrpos() finds the character position of the last match in a string.

iconv_strrpos() 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