English
German

PHP strpos

PHP String Position strpos

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


Searching a String with strpos

The way strpos works is it takes some string you want to search in as its first argument and another string, which is what you are actually searching for, as the second argument. If the function can find a search match, then it will return the position of the first match. However, if it can't find a match it will return false.

To make this function crystal clear, lets search a numbered, in-order string, for the number five.

Example:

$numberedString = "1234567890"; // 10 numbers from 1 to 0

$fivePosition = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePosition";
 

Output
The position of 5 in our string was 4

Notice that the position is 4, which may seem confusing at first, until you realize that PHP starts counting from 0.

    * The number 1 - Position 0 - No match
    * The number 2 - Position 1 - No match
    * The number 3 - Position 2 - No match
    * The number 4 - Position 3 - No match
    * The number 5 - Position 4 - Match

Although we only searched for a single character, you can use this function to search for a string with any number of characters. Also, it is important to note that this function will return the position of the start of the first match. So if we had searched the same string for "567890" we would again find a match and position 4 because that is where the match starts.


Finding All Occurrences in a String with 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 you would be none the wiser, unless you take action!

There is a third (optional) argument to strpos that will let you specify where to begin your search of the string. If you were to store the position of the last match and use that + 1 as an offset, you would skip over the first match and be find the next one.

Example:

$numberedString = "1234567890123456789012345678901234567890";

$fivePosition = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePosition";
$fivePosition2 = strpos($numberedString, "5", $fivePosition + 1);
echo "<br />The position of the second 5 was $fivePosition2";
 

Output
The position of 5 in our string was 4 The position of the second 5 was 14

By taking the first match's position of 4 and adding 1 we then asked strpos to begin searching after the last match. The string it was actually searching after computing the offset was: 6789012345... Letting us find the second 5 in the string.


PHP Tutorial,PHP strpos, PHP strpos example, learn PHP strpos,explain example PHP strpos online free training PHP Tutorial, PHP Tutorial example, learn PHP strpos, online tutorial, download tutorial, PHP Tutorial books, PHP Tutorial videos, live videos PHP Tutorial, learn PHP Tutorial, PHP Tutorial topic PHP strpos, live training PHP Tutorial, download free tutorial