views:

23

answers:

2

I ran into a little problem today when I was creating a really quick script to scan lines files in a user specified directory for //todo:...

So I had a line like this:

if (stripos($data, '//todo:')) { //case-insensitive search ^^
  //deal with the data appropriately
}

This did not find //todo: anywhere in any of the files! This was quite a surprise. I eventually ended up changing this line to remove the double forward slash (//) and it worked. Though this will now also match lines that are not comments that contain this string, perhaps not a common occurrence (and it'll likely never happen to me) but still possible.

I have no idea why this happened and would greatly appreciate an explanation.

A: 

I just ran this code and it returned 11 (as expected).

$data = "text test
//todo: fix me
text text";

echo stripos($data, '//todo:');

Are you sure that $data contains what you think it does? could there be some character encoding problems or maybe some escaping?

Tjofras
Yes I am 100% sure as when I print out data afterwards the // are there. Edit: I have discovered my problem from reading the answer above yours. It is the 0th element and as such I wasn't checking it correctly.
laconix
+2  A: 

Perhaps this is why?

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?

Maybe it has something to do with the stupid === operators being needed b/c == doesn't work right.

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

Brian T Hannan
Thanks, this will teach me to code after not sleeping the night before. -_-\
laconix