AppDividend
Latest Code Tutorials

PHP strpos() Example | PHP strpos() Function Tutorial

PHP strpos() Example | PHP strpos() Function Tutorial is today’s topic. The strpos() function finds a position of the first occurrence of a substring in the string. The strpos() function finds a position of the first occurrence of the string inside another string. The strpos() function is case-sensitive.This function is binary-safe.

PHP strpos() Example

Being able to manipulate the strings is a valuable skill, especially in PHP. You’ll most likely come across the programming problem that requires you to find some data in the string. The beginning of a lot of your string manipulation expertise will begin with a strpos function, which allows you to find the data in your string.

PHP strpos() method is used to find the position of the first occurrence of a string inside another string. As we have now known that it returns the position of the first occurrence of the string inside another string, or FALSE if the string is not found. One thing to remember that String positions start at 0, and not 1. See the following syntax.

strpos(string, find, start)

See the following example.

<?php

echo strpos("MBB is a great Actor in Stranger Things", "Stranger Things");

See the following output. I am inside the php folder, and we need to run the php app.php command to run the app.php file.

➜  php php app.php
24                                                                            
➜  php

When using strpos() ensure to do the comparison the right way.

With strpos() functions always use the identical operator “===” or “!==” and compare to “false“.

#When not to use strpos()

Do not use strpos() if you want to do the following things:

  1. Test for a pattern at the start or end of a string -> use preg_match()
  2. Extract multiple substrings -> use preg_match()
  3. Match a string for a complex pattern -> use preg_match()
  4. If you want to split a string a delimiters -> use strpbrk()

As strpos may return either FALSE (substring absent) or 0 (substring at start of string), strict versus loose equivalency operators must be used very carefully.

If we want to know that a substring is absent, you must use:  

=== FALSE

If we’re going to know that a substring is present (in any position including 0), you can use either of:

!== FALSE  (recommended)
> -1  (note: or greater than any negative number)

If we want to know that a substring is at the start of the string, you must use the following.

=== 0

If we want to know that a substring is in any position other than the start, you can use any of: 

> 0  (recommended)
!= 0  (note: but not !== 0 which also equates to FALSE)
!= FALSE  (disrecommended as highly confusing)

Also, note that you cannot compare a value of “” to the returned value of strpos. With a loose equivalence operator (== or !=) it will return results which don’t distinguish between the substring’s presence versus position. With a strict equivalence operator (=== or !==) it will always return false.

#Searching a string with strpos in PHP

Now, see the following code.

<?php

function Search($search, $string){ 
    $position = strpos($string, $search, 5);   
    if ($position == true){ 
        return "Found at position: " . $position; 
    } 
    else{ 
        return "Not Found"; 
    } 
} 

$string = "Millie Bobby Brown is fantastic Actor in ST3"; 
$search = "Bobby"; 
echo Search($search, $string);

In the above code example, if the substring is found, then it will return its index with defined text otherwise it will return just text “Not Found”.

The strpos only returns the FALSE as a boolean value, never TRUE. This means that

strpos() !== false

is a different beast then:

strpos() === true

Since the latter will never be true. After I found out, The warning in the documentation made a lot more sense.

#Finding all occurrences in a string with an offset

One of the limitations of strpos is that it only returns the position of the very first match. If there are 5,000 other matches in the string.

See the following program.

<?php

$ns = "1234567891011";

$pos = strpos($ns, "5");
echo "The position of 5 in our string was $pos"."\n";
$pos2 = strpos($ns, "5", $pos + 1);
echo "The position of the second 5 was $pos2";

See the following output.

➜  php php app.php
The position of 5 in our string was 4
The position of the second 5 was                                              
➜  php

Finally, PHP strpos() Example | PHP strpos() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.