Deoctor Posted December 7, 2009 Share Posted December 7, 2009 Hai i have a text file, which contains some information which gets created when i run a script. so the thing is how do i scan the entire txt file for a specific set of words whether they exist or not, is there any specific function in php Quote Link to comment https://forums.phpfreaks.com/topic/184238-how-do-i-scan-a-text-file-to-get-whether-the-specified-string-exists-or-not/ Share on other sites More sharing options...
Riparian Posted December 7, 2009 Share Posted December 7, 2009 There might be a better way but this is one way: $text_string = $yourtextfile; $findme = "the specific words"; $pos = strpos($text_string, $findme); if ($pos === false) { echo "The string $findme was not found in the string $text_string<br /><br />"; } else { echo "$findme found"; } Quote Link to comment https://forums.phpfreaks.com/topic/184238-how-do-i-scan-a-text-file-to-get-whether-the-specified-string-exists-or-not/#findComment-972655 Share on other sites More sharing options...
mikesta707 Posted December 7, 2009 Share Posted December 7, 2009 Riparian already answered the question, but incase your wondering, or didn't know, you can get the text of a file with file_get_contents(); $file = file_get_contents("whatever.txt"); if (strpos("search string", $file) === false){ echo "The string is not in file"); } Quote Link to comment https://forums.phpfreaks.com/topic/184238-how-do-i-scan-a-text-file-to-get-whether-the-specified-string-exists-or-not/#findComment-972656 Share on other sites More sharing options...
Deoctor Posted December 7, 2009 Author Share Posted December 7, 2009 10q for ur replies guys but the thing is it is not working as expected.. here is the code <?php $url_feed='http://tech2.in.com/rssfeed/rss_topstuff.xml'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://validator.w3.org/feed/check.cgi?url=$url_feed"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); $output = curl_exec($ch); $fh = fopen("out.txt", 'w'); fwrite($fh, $output); $value='img alt="[Valid RSS]" title="Valid RSS" src="images/valid-rss.png" /> This is a valid RSS feed.'; $file=file_get_contents("out.txt"); if (strpos($value,$file) === false) { echo ("The string is not in file"); } fclose($fh); curl_close($ch); ?> in the out.txt file i need to search for the $value.. Quote Link to comment https://forums.phpfreaks.com/topic/184238-how-do-i-scan-a-text-file-to-get-whether-the-specified-string-exists-or-not/#findComment-972662 Share on other sites More sharing options...
premiso Posted December 7, 2009 Share Posted December 7, 2009 strpos You have your parameters mixed up: if (strpos($file, $value) === false) Will work like you want it to. Quote Link to comment https://forums.phpfreaks.com/topic/184238-how-do-i-scan-a-text-file-to-get-whether-the-specified-string-exists-or-not/#findComment-973036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.