Jump to content

Searching a text file for a string


thyscorpion

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
Link to comment
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]

Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.