gabrielalcivar Posted December 20, 2008 Share Posted December 20, 2008 Hi all! Ok, I've done a few searches and spend about the last 2 hours trying to figure out what's wrong. I upload a file and I want to search in it for a string line by line for a string: (;;{"t":"msg","c":" function chatparser($fname) { $needle='(;;{"t":"msg","c":"'; $file = fopen($fname, "r") or exit("Filename ERROR - Unable to open file: ".$fname); while(!feof($file)) { $lnc++; $chatstr= fgets($file,4096); if (strlen(stristr($chatsrt, $needle))>0) { echo "Stristr: ".strlen(stristr($chatstr, $needle))." <b>Line: </b>$lnc <br>"; } else { echo "Stristr: ".strlen(stristr($chatstr, $needle))." <b>Line: </b>$lnc <br>"; } } I also tried this: $lines = file($fname); foreach ($lines as $chastr) { $lines = file($fname); foreach ($lines as $chastr) { echo "Stristr: ".strlen(stristr($chatstr, $needle))." <b>Line: </b>$lnc <br>"; } } In both cases I get a a value of "0" although in text file there's a line including the word the search string. Text File contents: Content-Length: 314 Connection: Keep-Alive\r\n \r\n Line-based text data: text/html [truncated] for (;;{"t":"msg","c":"p_500 Thanks for the help in advance. Link to comment https://forums.phpfreaks.com/topic/137767-fopenfile-and-stristr/ Share on other sites More sharing options...
aseaofflames Posted December 20, 2008 Share Posted December 20, 2008 i don't know know if you just typed it wrong on here, but if (strlen(stristr($chatsrt, $needle))>0) { should be if (strlen(stristr($chatstr, $needle))>0) { and for the second method why are there two foreach statements? also in the foreach $chastr should be $chatstr Link to comment https://forums.phpfreaks.com/topic/137767-fopenfile-and-stristr/#findComment-720172 Share on other sites More sharing options...
jonsjava Posted December 20, 2008 Share Posted December 20, 2008 i don't know know if you just typed it wrong on here, but if (strlen(stristr($chatsrt, $needle))>0) { should be if (strlen(stristr($chatstr, $needle))>0) { am I getting cross-eyed, or are those the exact same thing? Link to comment https://forums.phpfreaks.com/topic/137767-fopenfile-and-stristr/#findComment-720173 Share on other sites More sharing options...
redarrow Posted December 20, 2008 Share Posted December 20, 2008 there not the SAME LOOK AGIN $chatstr << CLUE Link to comment https://forums.phpfreaks.com/topic/137767-fopenfile-and-stristr/#findComment-720182 Share on other sites More sharing options...
jonsjava Posted December 20, 2008 Share Posted December 20, 2008 there not the SAME LOOK AGIN $chatstr << CLUE Eye see. Must be getting tired.... Link to comment https://forums.phpfreaks.com/topic/137767-fopenfile-and-stristr/#findComment-720183 Share on other sites More sharing options...
gabrielalcivar Posted December 21, 2008 Author Share Posted December 21, 2008 Thanks it works now...that's why is good to stop when you work for too long the 2 foreach's are actually a copy/paste mistake (again tiredness) Link to comment https://forums.phpfreaks.com/topic/137767-fopenfile-and-stristr/#findComment-720602 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.