Jump to content

how do i scan a text file to get whether the specified string exists or not


Deoctor

Recommended Posts

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

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";

}

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");
}

10q for ur replies guys

but the thing is it is not working as expected.. :P

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

 

Archived

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

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