Table of Content
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
===so0does not look likefalse.
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"); // 0PHP treats uppercase and lowercase as different. “h” and “H” do not match.
Find the first occurrence:
echo strpos("abc abc abc", "abc"); // 0It 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
striposwhen 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 = "test@example.com";
echo substr($email, strpos($email, "@") + 1); // example.comThis 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, ":")); // langThis 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"; // YesIt 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
strposto find where one string appears in another. - It gives
0or higher if it finds it,falseif it does not. - Use
striposwhen you want to skip case checks. - Combine with
substrto cut or extract text.
FAQs
What does strpos return if it finds the match at the start?
Is strpos case-sensitive?
How can I extract part of a string after a certain word?
Can strpos find the last match?
Why does strpos return false even when I see the word?
Similar Reads
The IF statement in PHP helps to see if a condition is true or not. If it is true, it…
In PHP, manipulation of XML data can be very critical when most systems' data exchange relies on the format. XML—Extensible…
The term $_POST is quite familiar territory when one works with forms in PHP. This tutorial talks about what $_POST does, how to use…
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…
User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…
The array_rand() is a helper function in PHP that makes it easier when dealing with arrays; you can just pull…
PHP gives us the ability to work with MongoDB, highlighting the importance of CRUD (Create, Read, Update, Delete) operations for…
The PHP do-while loop is a type of loop that executes a block of code at least once, and then…
The array_key_last gives you the last key in an array and works with numeric and associative arrays in PHP. It…
let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…