Strpos Counts From Which Pos?

Hiya,

http://www.tizag.com/phpT/php-string-strpos.php

On the above link, it is stated:

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.

I do not understand the last part and so can someone show an example:
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.

Oh, I now understand. It meant the string “567890” starts from position 4.
On previous example, it was searching for the single character 4’s position. This time, it is searching where the string starts.

Ok. I understand that with strpos, you can only find the first match of a character or string but not any further than that if there were more than one match. And to fix this, I can use the incrementer to set the next starting point. Like so:

$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";

So, naturally strpos has 2 parameters if you want to find the pos of only one match or the first match. Right ?
And, if you want to find all the matches’ positions then you add the final matched position on the 3rd parameter. Right ?

The answers you seek are in the documentation

http://php.net/manual/en/function.strpos.php

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

5 Likes

Good. But, I’m avoiding the reference manual, like the plague as it does my head in. But, I agree, I should refer back to it from time to time.

Php has a very good and especially up to date manual. There are usually many relevant examples which are far better than many other tutorials. Frequently I read a tutorial then compare with the php manual to ensure the tutorial is valid.

Learn to love the manual :slight_smile:

1 Like

I think the error you made earlier was to treat the manual as a tutorial to learn from. That’s not what it’s good for.
But it is excellent as a reference for times like when you want to know exactly how a particular function works.
So if I was wanting to know how strpos works, the very first thing I would do is look it up in the manual.
It is a valuable tool for any PHP developer and should be in everyone’s bookmarks.

5 Likes

Well spoken and I had already planned to do that.

This thread can now be closed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.