DaniIvanov Posted October 18, 2012 Share Posted October 18, 2012 (edited) Hello I have an issue with strpos. I have the following code function participantIdFilter ($participantID) $log = "uploads/log.txt"; $logRead = fopen($log, "r"); $content = file_get_contents($log); if(strpos($content, $participantID)){ $search = "TTRUE"; } else { $search = "FFALSE"; } } The strpos ALWAYS returns false no matter what I do. I know for sure that both $content and $participantID have strings in them. When I change the $participantID within the strpos perimeters with "integers/numbers/ids" it works as expected but as soon as i put in the variable it always result in false. What I am trying to do is open a file and check if participantID is in the file and if it is generate a new one. Edited October 18, 2012 by DaniIvanov Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/ Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2012 Share Posted October 18, 2012 Show us how you're calling the function, and the data you're giving it. Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386180 Share on other sites More sharing options...
DaniIvanov Posted October 18, 2012 Author Share Posted October 18, 2012 (edited) Show us how you're calling the function, and the data you're giving it. Here is how I am calling the function // Check if participantID already exists and return TRUE or FALSE $some = participantIdFilter(3374); echo $some; When i know that it works I will change 3374 with a variable that generates a rand number. I use 3374 right now because i know for certain that it already exists in the LOG file) Here is the FULL function. I cut out a small piece on the end (of the top post) because i did not think that it was relevant for now function participantIdFilter ($participantID) { $a = $participantID; $log = "uploads/log.txt"; $logRead = fopen($log, "r"); $content = file_get_contents($log); if(strpos($content, $a)){ $search = "TTRUE"; } else { $search = "FFALSE"; } return $search . "/<br />" . $content; if($search == 1) { $participantID = rand(1000, 9999); } } Edited October 18, 2012 by DaniIvanov Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386185 Share on other sites More sharing options...
kicken Posted October 19, 2012 Share Posted October 19, 2012 Note that if your search item is the very first thing in your file, you will get a false result because strpos will return 0 (found at position zero) which is loosely equal to false (which is returned when not found). The proper way to test for whether the item is found or not would be: if (false !== strpos($content, $a)){ $search = "TTRUE"; } else { $search = "FFALSE"; } Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386186 Share on other sites More sharing options...
DaniIvanov Posted October 19, 2012 Author Share Posted October 19, 2012 Note that if your search item is the very first thing in your file, you will get a false result because strpos will return 0 (found at position zero) which is loosely equal to false (which is returned when not found). The proper way to test for whether the item is found or not would be: if (false !== strpos($content, $a)){ $search = "TTRUE"; } else { $search = "FFALSE"; } I just tried that (also before) and it did not work. It gives FFALSE always even if the value that i pass in to a exists in the log file. Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386191 Share on other sites More sharing options...
kicken Posted October 19, 2012 Share Posted October 19, 2012 use var_dump($content) and var_dump($a) to verify that those variables contain what you think they contain. Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386194 Share on other sites More sharing options...
DaniIvanov Posted October 19, 2012 Author Share Posted October 19, 2012 Yes they both contain the data that I expect them to. Just checked them with var_dump. Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386200 Share on other sites More sharing options...
kicken Posted October 19, 2012 Share Posted October 19, 2012 needle If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. Since you're passing an int, strpos is searching for the character defined by chr($a), not (string)$a. if (false !== strpos($content, (string)$a)){ side note your call to fopen() is completely unnecessary and can be removed. Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386207 Share on other sites More sharing options...
DaniIvanov Posted October 19, 2012 Author Share Posted October 19, 2012 Thanks now it works very well Quote Link to comment https://forums.phpfreaks.com/topic/269653-strpos-issue/#findComment-1386222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.