PHP strpos Function: How it Works with Examples

php strpos function

Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result in a new line or a position that does not match what you had in mind.

What the strpos Function Is in PHP

strpos means “string position.” It looks for one string inside another and gives the location where it starts.

Here is the syntax:

strpos($haystack, $needle, $offset)

You provide the main text, what you want to find, and optionally where to start looking.

  • It returns the index (position) where the match begins.
  • If it does not find a match, it gives false.
  • To avoid confusion, check it with === so 0 does not look like false.

Here is an example:

echo strpos("apple pie", "pie"); // 6

“pie” starts at position 6 inside “apple pie”.

echo strpos("banana", "a"); // 1  

The first “a” appears at position 1. It only shows the first match.

Case-sensitive behavior:

echo strpos("Hello", "h"); // false  
echo strpos("Hello", "H"); // 0

PHP treats uppercase and lowercase as different. “h” and “H” do not match.

Find the first occurrence:

echo strpos("abc abc abc", "abc"); // 0

It finds “abc” three times but only returns the position of the first one.

Handle Case-Insensitive

strpos sees “Test” and “test” as different. Use stripos when you want to ignore the case.

It acts just like strpos, but does not care about capital letters. For example:

echo stripos("Make it Simple", "simple"); // 8

“Simple” starts with a capital letter, but stripos still finds “simple” inside it.

  • Use stripos when user input may use upper or lower case.
  • This avoids missed matches from small changes.
  • It makes your code more flexible and less strict.

When you check text, decide whether case matters. If it does not, use stripos. That keeps your logic clear and avoids false results. It leads to bugs that take time to trace if you skip this.

Combine strpos with Other String Functions

You can mix strpos with substr to cut out parts of text after a match.

$text = "user:john";
$pos = strpos($text, ":");
$name = substr($text, $pos + 1); // "john"

This grabs the part of the string after the colon. It helps break values apart.

You can also clean and trim the result:

$data = "  note=important ";
$start = strpos($data, "=");
$value = trim(substr($data, $start + 1)); // "important"

trim() clears spaces, substr() grabs the value after the =, and strpos() finds the position.

Examples

Find the domain from the email:

$email = "[email protected]";
echo substr($email, strpos($email, "@") + 1); // example.com

This cuts everything after the @ symbol. It shows the domain of the email.

Get part before a colon:

$line = "lang:PHP";
echo substr($line, 0, strpos($line, ":")); // lang

This slices the text before the colon. It gives the key from a key:value pair.

Check if the sentence starts with a word:

$sentence = "Welcome to the show";
echo strpos($sentence, "Welcome") === 0 ? "Yes" : "No"; // Yes

It checks if “Welcome” starts at position 0. That tells you if the string begins with that word.

Wrapping Up

In this article, you learned how to use strpos to find parts of strings. You also saw how to work with stripos, substr, and trim.

Here is a quick recap:

  • Use strpos to find where one string appears in another.
  • It gives 0 or higher if it finds it, false if it does not.
  • Use stripos when you want to skip case checks.
  • Combine with substr to cut or extract text.

FAQs

What does strpos return if it finds the match at the start?

It returns 0. You must use === to check this, or it may look like false.

Is strpos case-sensitive?

Yes. If you want to ignore case, use stripos instead.

How can I extract part of a string after a certain word?

Use strpos to find where the word starts, then use substr to cut the rest.

Can strpos find the last match?

No, it only finds the first. Use strrpos if you want the last one.

Why does strpos return false even when I see the word?

Check the letter case. "hello" is not the same as "Hello" to strpos.

Similar Reads

PHP Math: Essential Functions with Examples

PHP math may sound like a pretty simple thing, but I assure you it's a set of tools that will…

How to Install PHP on a Raspberry Pi

You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…

PHP $_SESSION: Secure Your Web Applications in PHP

It’s very important to remember user data for each session when building web applications. This enables a high level of…

PHP filter_id Function: How to Retrieve PHP Filter IDs

Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP…

PHP Variadic Functions: Use Unlimited Arguments

PHP Variadic functions show you a way to handle a variable number of arguments within a function. They are designed…

MongoDB CRUD with PHP: Create, Read, Update, and Delete

PHP gives us the ability to work with MongoDB, highlighting the importance of CRUD (Create, Read, Update, Delete) operations for…

Understanding the PHP Operator Precedence

The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…

PHP fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

PHP strict_types: How the Strict Mode Works

If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…

How to Delete Documents in MongoDB with PHP

Deleting records in MongoDB is a common task. You might need to clear old data, remove outdated entries, or handle…

Previous Article

PHP Hello World: Write Your First Program with Code Example

Next Article

PHP strtoupper Function: Convert Strings to Uppercase

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.