Jump to content


Photo

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


  • Please log in to reply
8 replies to this topic

#1 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 24 July 2006 - 07:55 PM

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:
$filename ="downloadedMovies.txt";     

$myFile = fopen($filename, "r"); //open the file for reading, file pointer will be at the beginning of the file 
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...
:)

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 24 July 2006 - 08:06 PM

try glob()
http://us2.php.net/m...nction.glob.php
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 24 July 2006 - 08:06 PM

<?php
foreach(glob("path/to/files/*.txt") as $file) {
    echo file_get_contents($file);
}
?>

EDIT: Beaten to it! (Again!)

#4 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 24 July 2006 - 08:44 PM

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

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 24 July 2006 - 09:00 PM

you can put all the results into an array, then use array_rand to randomly select a record.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 25 July 2006 - 08:37 AM

you can put all the results into an array, then use array_rand to randomly select a record.

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


#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 25 July 2006 - 08:43 AM

The levenshtein() function might be of some use to you...

#8 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 26 July 2006 - 08:59 AM

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?

#9 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 27 July 2006 - 11:28 AM

<?php
$str = str_replace(" ","-",$str).".txt";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users