About Me

Thursday 19 April 2012

PHP - String Position - 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.

PHP Code:

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

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

Display:

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.

PHP Code:

$numberedString = "1234567890123456789012345678901234567890";

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

Display:

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.
If we use our knowledge of PHP While Loops we can find every single 5 in our string numberedString with just a few lines of code.

PHP Code:

$numberedString = "1234567890123456789012345678901234567890";
$offset = 0; // initial offset is 0
$fiveCounter = 0;

// First check if there is a "5" at position 0.
if(strpos($numberedString, "5") == 0){
 $fiveCounter++;
 echo "<br />Five #$fiveCounter is at position - 0";
}

// Check the rest of the string for 5's
while($offset = strpos($numberedString, "5", $offset + 1)){
 $fiveCounter++;
 echo "<br />Five #$fiveCounter is at position - $offset";
}

Display:

Five #1 is at position - 4
Five #2 is at position - 14
Five #3 is at position - 24
Five #4 is at position - 34
That conditional statement in our while loop may look a little intimidating, but not if you break it down.
  • $offset = strpos($numberedString, "5", $offset + 1) - This is our conditional statement for our PHP While Loop. If this ever is false the while loop will stop running. This conditional statement always runs before each pass through the while loop.
  • strpos($numberedString, "5", $offset + 1) - This is the same code we used in a previous example. We are going to search our string numberedString for the number 5 and use the last match's value (stored in $offset) + 1 to skip over the last match. The first $offset we use has a value of 0, so that we start at the beginning of the string.
  • $offset = strpos(... We are going to store the location returned by strpos into $offset so that we can skip this match the next time the while loop runs through the code. If strpos ever fails to find a match then this will be set to false making our while loop stop executing.

0 comments:

Post a Comment