thyscorpion Posted March 25, 2006 Share Posted March 25, 2006 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 https://forums.phpfreaks.com/topic/5811-searching-a-text-file-for-a-string/ Share on other sites More sharing options...
toplay Posted March 25, 2006 Share Posted March 25, 2006 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 https://forums.phpfreaks.com/topic/5811-searching-a-text-file-for-a-string/#findComment-20703 Share on other sites More sharing options...
thyscorpion Posted March 25, 2006 Author Share Posted March 25, 2006 hey thanks.. Toplay.... I used the Strstr function. as i use PHP 4 .. i could not use stripos Func.. lolthanks 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 https://forums.phpfreaks.com/topic/5811-searching-a-text-file-for-a-string/#findComment-20720 Share on other sites More sharing options...
toplay Posted March 25, 2006 Share Posted March 25, 2006 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. Link to comment https://forums.phpfreaks.com/topic/5811-searching-a-text-file-for-a-string/#findComment-20723 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.