thyscorpion Posted July 24, 2006 Share Posted July 24, 2006 hi ???i have a site in which i want the php code to search a specified dir on my site containing TXT files. The search will be with a search string i give.i have been till now picking up txt files when i know a file name. but dont know how to search for a file according to a given keyword (or a part of keyword). Please help me out as soon as possible. as i need the site up and running in 2 days. :-S.was using something like this:[code]$filename ="downloadedMovies.txt"; $myFile = fopen($filename, "r"); //open the file for reading, file pointer will be at the beginning of the file [/code]but now i want to use a simple Include command. :-).. hope someone can help me at the earliest.. dont worry i can do the codding myself but need to know how.. dont know the syntax and all.. :-)thanks... :) Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/ Share on other sites More sharing options...
ryanlwh Posted July 24, 2006 Share Posted July 24, 2006 try glob()http://us2.php.net/manual/en/function.glob.php Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63113 Share on other sites More sharing options...
zq29 Posted July 24, 2006 Share Posted July 24, 2006 [code]<?phpforeach(glob("path/to/files/*.txt") as $file) { echo file_get_contents($file);}?>[/code][b]EDIT:[/b] Beaten to it! (Again!) Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63114 Share on other sites More sharing options...
thyscorpion Posted July 24, 2006 Author Share Posted July 24, 2006 Thanks a ton!.. :-).. ok one more Question. hope i am not bugging you thou....like in some cases i have say three text files with the same keyword say : alpha_1.txt alpha_2.txt and alpha_3.txt and the keyword being 'alpha'.i needed a way to pick up anyone of the three txt files in random. ???thank you for your quick replies.. Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63123 Share on other sites More sharing options...
ryanlwh Posted July 24, 2006 Share Posted July 24, 2006 you can put all the results into an array, then use array_rand to randomly select a record. Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63127 Share on other sites More sharing options...
thyscorpion Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=ryanlwh link=topic=101731.msg402866#msg402866 date=1153774816]you can put all the results into an array, then use array_rand to randomly select a record.[/quote]hi thanks.. i needed something like: if the keyword is: "manic monday"the script should search for a "manic-monday.txt" and if not found ( no exact matches) . it should get teh next best match example: "manic.txt".i agree putting them into the array is the right step forward but how to search in this way?please help me out on this... Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63287 Share on other sites More sharing options...
zq29 Posted July 25, 2006 Share Posted July 25, 2006 The levenshtein() function might be of some use to you... Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63288 Share on other sites More sharing options...
thyscorpion Posted July 26, 2006 Author Share Posted July 26, 2006 thanks. levenshtein() does work. hurraay! :)can anyone help me how to change a string with a value like: "alpha beta gama" to "alpha-beta-gama.txt" ??? in php? Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-63866 Share on other sites More sharing options...
zq29 Posted July 27, 2006 Share Posted July 27, 2006 [code]<?php$str = str_replace(" ","-",$str).".txt";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15534-how-to-search-a-dir-and-display-a-txt-file-with-php/#findComment-64600 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.