godsent Posted January 29, 2009 Share Posted January 29, 2009 Then im trying to search some name in txt file it never founds, i think i made a mistake somewhere, but i can't find it. function searchForIt($user) { $pfile = fopen('names.txt',"r"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode(', ', $line); if ($tmp[0] == $user) { echo "FOUND!"; } else { echo "not found!"; } } } names file look like this Winten, Acme, Admin, User, Lover, Super, asap, AU, asd, asdfasdf, asdas, Array, Apache, Sk.Winten, wintenas, wintensk, wintenaaaa, w!nten, If you know what i did wrong please post, :-\ Link to comment https://forums.phpfreaks.com/topic/142938-php-searching-in-txt-file-error-please-help/ Share on other sites More sharing options...
cwarn23 Posted January 29, 2009 Share Posted January 29, 2009 Your script was a bit hard to understand because of the use of php4 file read functions. But if you have php5 then the following should work: $filedata=file_get_contents('names.txt'); $filedata.=' '; $tmp=explode(', ',$filedata); foreach($tmp AS $tmp) { if ($tmp==$user) { echo "FOUND"; $found=1; break; } } if ($found!==1) { echo "not found!"; } unset($tmp); Link to comment https://forums.phpfreaks.com/topic/142938-php-searching-in-txt-file-error-please-help/#findComment-749461 Share on other sites More sharing options...
Errant_Shadow Posted January 29, 2009 Share Posted January 29, 2009 This sounds like a good job for XML. this tutorial might help you understand ( http://www.webmonkey.com/tutorial/Get_Started_With_SimpleXML_in_PHP ) if not, there are plenty of ways to do this with php and a txt file as well *nods* Link to comment https://forums.phpfreaks.com/topic/142938-php-searching-in-txt-file-error-please-help/#findComment-749474 Share on other sites More sharing options...
Mchl Posted January 29, 2009 Share Posted January 29, 2009 Hard to say without seeing your file. But if you wan to compare all elements in each line, you'd need something like this. function searchForIt($user) { $pfile = fopen('names.txt',"r"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode(', ', $line); foreach($tmp AS $key => $value) { if ($value == $user) { echo "FOUND!"; } else { echo "not found!"; } } } } Link to comment https://forums.phpfreaks.com/topic/142938-php-searching-in-txt-file-error-please-help/#findComment-749537 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.