www.retosphere.de

PHP string finding (using strpos())

Home
Projects
News
Links
GuestBook
Contact
Sometimes, I have to use the function strpos() to find out if e.g. the string "Where is my bag?" contains "bag" or anything similar...

Table of contents:
1. Definition and examples of strpos()
2. Return value of strpos() and workaround
3. The "identical"-operator ===
4. Return type of strpos()


   Definition: int strpos (string haystack, string needle [, int offset])

Now, see what strpos() does (starting with an example):

 
<?php
$MyString 
"abcdefg";
echo 
strpos($MyString"cde");
?>
This example finds the substr (needle) "cde" in the string $MyString.
The result is: 2 (a is $MyString[0], b is $MyString[1], c is $MyString[2])
show me
 
 
<?php
$MyString 
"abcdefg";
echo 
strpos($MyString"abc");
?>
And this finds "abc" in $MyString.
Result is: 0 ("abc" is at position 0 in $MyString)
show me
  
 
<?php
$MyString 
"abcdefg";
echo 
strpos($MyString"abc");
echo 
"-";
$res strpos($MyString"xyz");
echo 
$res;
echo 
"<br>";
if (isset(
$res)) echo "Variable is set, content of Variable: $res<br>";
if (empty(
$res)) echo "Variable is empty";
?>
Before v4.03b this example displayed 0 - 0. So, when a substr is found on the first position of the string, it displayed 0 equally to the case when the substr is not found.
Since v4.03b this will display "0 -", the second (search for "xyz") returns empty (but the variable is set).
This change makes it besser to distinguish, whether a substr has been found or not. We could e.g. ask if the variable $res was empty if "0" would not be empty (check the empty-problem).
show me

  The new workaround for this is:
 
<?php
$MyString 
"abcdefg";

$pos strpos ($MyString"abc");
if (
$pos === false// 3 equal signs !!!
    
echo "not found!";
else
    echo 
"found the substr!";
?>
Okay, this makes it definitly better. You can successfully distinguish the two cases.
show me

But what the heck does this "===" mean? Why do I have to create such an operator? To me, it seems that this is just a dirty workaround.
=> The "===" means identical. That way you can check whether two variables are identical (=of same type and same content).
 
<?php
if (5=="5") echo '5 equals "5"<br>'."\n";
if (
5==="5") echo 'and is identical';
else echo 
'but 5 is <b>not identical</b> with "5"';
?>
show me
 
Of course, this makes sense but what do you need it for in a language that is NOT strong-typed?

At last, you do not need it here:
In C/C++ functions that do not find anything or produce an error, return a negative value! Something like:
 
<?php
$MyString 
"abcdefg";

$pos strpos ($MyString"xyz");
if (
$pos 0)  // you can never have a string at a position < 0,
                // so it has not been found
    
echo "not found!";
else
    echo 
"found the substr!";
?>
would make it very very much better and easier and even more readable!
 
  There's another reason, why the behaviour of strpos() is so bad. Let me show you using a program in C-style:
 
<?c
int strpos(String haystack, String needle) // function returns an int    
    // forgetting offset and using object String
    // for easier programming...
    {
    // ... look if I can find it
    if (found) 
        return pos_where_found
    else 
        return (boolean) false;    
        // see the typecast? 
        // It is unnecessary, but I used it to make
        // clear what happens.
    }
?>
Do you see what happens? The function either returns an integer or a boolean in dependency of the input-values.
You can produce functions that return different types by overloading, using different or a different number of parameters, but here it only depends on what you use as values for the function-parameters.
e.g. the call strpos("abc", "bc") returns an integer, but strpos("abc", "xy") returns a boolean. Have you ever seen anything alike? And if you have, have you ever seen anything alike where it is so very easy to make things clearer?
 
I will never understand ... :(

Please check the section PHP types, too.


Top
this page last updated: 28.11.2013
Top