Jump to content

Archived

This topic is now archived and is closed to further replies.

thyscorpion

how to search a dir and display a txt file with php ???

Recommended Posts

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...
:)

Share this post


Link to post
Share on other sites
[code]<?php
foreach(glob("path/to/files/*.txt") as $file) {
    echo file_get_contents($file);
}
?>[/code]

[b]EDIT:[/b] Beaten to it! (Again!)

Share this post


Link to post
Share on other sites
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.. 

Share this post


Link to post
Share on other sites
you can put all the results into an array, then use array_rand to randomly select a record.

Share this post


Link to post
Share on other sites
[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...

Share this post


Link to post
Share on other sites
The levenshtein() function might be of some use to you...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[code]<?php
$str = str_replace(" ","-",$str).".txt";
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.