hansford Posted June 13, 2008 Share Posted June 13, 2008 I have (2) text files. The code is suppose to open logs2.txt and read a line from the file, it then opens files2.txt and iterates through each line to see if theres a matching substring. If one is found then it just echos out what was found. The code doesn't work as expected. It finds 2 occurenses of matching strings, but I know there's at least 4. I've tried addslashes(), preg_quote() with no luck. I've checked each value of the 2 variables before they enter str_pos() and they should find a match, but they don't. I know I can use preg_match for this, but I'm curious as to why this is not working. http://www.modernterrain.com/files2.txt http://www.modernterrain.com/logs2.txt I have the follwing code ------------------------- <? $logs=fopen("logs2.txt", r); while (!feof($logs)) { $tLogs=fgets($logs); $text=fopen("files2.txt", r); while (!feof($text)) { $tText=fgets($text); if(strpos($tLogs,$tText)){ echo("Found {$two}<br>"); break; } } fclose($text); } fclose($logs); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110107-strpos-problem/ Share on other sites More sharing options...
jonsjava Posted June 13, 2008 Share Posted June 13, 2008 I did it this way, and made it work: <?php $content1 = "127.0.0.1 - - [15/May/2008:04:03:23 -0400] \"GET /helpdesk/Admin/index.cfm HTTP/1.1\" 301 330 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14\" 127.0.0.1 - - [15/May/2008:04:03:23 -0400] \"GET /helpdesk/admin/styles.css HTTP/1.1\" 200 2305 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14\" 127.0.0.1 - - [15/May/2008:04:03:23 -0400] \"GET /index.cfm?PID=3298 HTTP/1.1\" 200 19268 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14\""; $content2 = "/helpdesk/admin/images/arrow_up.gif /index.cfm /helpdesk/Admin/index.cfm /helpdesk/admin/images/minus.gif /helpdesk/admin/styles.css"; $array1 = explode("\n", $content1); $array2 = explode("\n", $content2); foreach ($array2 as $value){ foreach ($array1 as $value2){ if (strpos($value2, $value)){ print "found $value\n"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/110107-strpos-problem/#findComment-565077 Share on other sites More sharing options...
Barand Posted June 13, 2008 Share Posted June 13, 2008 be careful with your usage of strpos <?php $str = 'abcde'; if (strpos($str, 'abc')) { echo "found it"; } else { echo "not found"; // this prints !!! } ?> You need if (strpos($str, 'abc') !== false) Quote Link to comment https://forums.phpfreaks.com/topic/110107-strpos-problem/#findComment-565083 Share on other sites More sharing options...
jonsjava Posted June 13, 2008 Share Posted June 13, 2008 be careful with your usage of strpos <?php $str = 'abcde'; if (strpos($str, 'abc')) { echo "found it"; } else { echo "not found"; // this prints !!! } ?> You need if (strpos($str, 'abc') !== false) but, if you do if(strpos($str, 'abc')){ //this } else{ //that } it's looking for any value other than false. From PHP.net: Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE. EDIT theory isn't the same as practice. ok, it doesn't work the way documented. thx for the clarifcation. Quote Link to comment https://forums.phpfreaks.com/topic/110107-strpos-problem/#findComment-565087 Share on other sites More sharing options...
DarkWater Posted June 13, 2008 Share Posted June 13, 2008 You need !== FALSE because strpos returns 0 if it finds the string at the 0th position, so you need to use to identity quality operator to make sure it's FALSE and not 0. Quote Link to comment https://forums.phpfreaks.com/topic/110107-strpos-problem/#findComment-565090 Share on other sites More sharing options...
hansford Posted June 14, 2008 Author Share Posted June 14, 2008 ok I now have !==false and it can only find 1 out of the possible 4 it should find. If it were a problem with the double quotes, then it wouldn't find anything. If it were a problem with the way chars are compared, it wouldn't find anything. Baffling or maybe just buggy, but I couldn't find any reports for this. <? $logs=fopen("logs2.txt", r); while (!feof($logs)) { $tLogs=fgets($logs); $text=fopen("files2.txt", r); while (!feof($text)) { $tText=fgets($text); if(strpos($tLogs,$tText) !==false){ echo("Found {$two}"); break; } } fclose($text); } fclose($logs); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110107-strpos-problem/#findComment-565298 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.