Jump to content

Archived

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

thyscorpion

Searching a text file for a string

Recommended Posts

hi, i am very new to php and my page requires a search function so that i can show only those lines from my text file matching to the search string. even if a part of the line in teh text in equal to the search string i need to echo it.
just to show u what i haved done so far:-

[code]
$fcontents = file($filename);


while (list ($line_num, $line) = each ($fcontents)) { // while there are elements in the array
  // remove the P tag from each line and print it
      echo "<b>Movie $line_num:</b> " . str_replace("<p>", "", $line) . "<br>\n";  
}
      
fclose($myFile);
[/code]
currently as u can see i am echoing all the lines. now i need i have added a search option so that only the searched string matches are displayed..
help plz.
i use PHP4. (my web host)
I just can't find a function in php to search a string for a string.

plz help asap..
thanks a ton
-Thyscorpion

Share this post


Link to post
Share on other sites
For what you need, any of these type of functions will work:

[a href=\"http://us2.php.net/manual/en/function.strstr.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.strstr.php[/a]

[a href=\"http://us2.php.net/manual/en/function.stripos.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.stripos.php[/a]

Otherwise, for more sophisticated searching, the use of regex or PREG will be more powerful (but sometimes more complicated to implement).

[a href=\"http://us2.php.net/manual/en/function.ereg.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.ereg.php[/a]

[a href=\"http://us2.php.net/manual/en/function.preg-match.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.preg-match.php[/a]

Share this post


Link to post
Share on other sites
hey thanks.. Toplay....
I used the Strstr function. as i use PHP 4 .. i could not use stripos Func..
lol

thanks a ton.. ciao...

[!--quoteo(post=358380:date=Mar 25 2006, 05:24 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Mar 25 2006, 05:24 PM) [snapback]358380[/snapback][/div][div class=\'quotemain\'][!--quotec--]
For what you need, any of these type of functions will work:

[a href=\"http://us2.php.net/manual/en/function.strstr.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.strstr.php[/a]

[a href=\"http://us2.php.net/manual/en/function.stripos.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.stripos.php[/a]

Otherwise, for more sophisticated searching, the use of regex or PREG will be more powerful (but sometimes more complicated to implement).

[a href=\"http://us2.php.net/manual/en/function.ereg.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.ereg.php[/a]

[a href=\"http://us2.php.net/manual/en/function.preg-match.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.preg-match.php[/a]
[/quote]

Share this post


Link to post
Share on other sites
Sorry, I meant to post strpos() but it is case sensitive.

[a href=\"http://us2.php.net/manual/en/function.strpos.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.strpos.php[/a]

Glad to help.

Happy coding.

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.