search a text file for a particular word

Problems with the Windows version of XAMPP, questions, comments, and anything related.

search a text file for a particular word

Postby ym_chaitu » 04. December 2009 15:20

Hai
is there any command in the php which will search for a particular keyword in the txt file and if the a single instance of the word is found then it will display a message or else not..
--
Chaitanya Babu Yanamadala
www.drvirusindia.co.cc
User avatar
ym_chaitu
 
Posts: 33
Joined: 13. November 2009 08:26
Location: Bangalore,India

Re: search a text file for a particular word

Postby Wiedmann » 04. December 2009 15:34

Define "single instance of the word".
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: search a text file for a particular word

Postby ym_chaitu » 04. December 2009 15:39

Wiedmann wrote:Define "single instance of the word".

like if scan a
Code: Select all
<?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.';
$section = file_get_contents('./out.txt', NULL, NULL, 1256, 95);
/*echo $section;
$fh1=fopen("out_1.txt",'w');
fwrite($fh1, $section);
fclose($fh1);
var_dump($section);*/
fclose($fh);
curl_close($ch);?>

this out.txt file the required single instance of word i require is $value, when i change the $url_feed the word will appear in the different place i mean in a different character count. i hope u got it out..
so i want a function instead of file_get_contents which will scan the entire txt file and will check for the $value and if it is there then it should display ok
if u require any other info just ping me back
--
Chaitanya Babu Yanamadala
www.drvirusindia.co.cc
User avatar
ym_chaitu
 
Posts: 33
Joined: 13. November 2009 08:26
Location: Bangalore,India

Re: search a text file for a particular word

Postby Stepke-DSL » 04. December 2009 15:42

Try this:

Code: Select all
$count      =    explode($yourword, $yourstring);

if (count($count) == 2)
    {

    // the word $yourword is in $yourstring (just 1 word)

    }
elseif (count($count) > 2)
    {

    // the word $yourword is in $yourstring (more than 1 word)

    }
else
    {

    // the word $yourword is NOT in $yourstring

    }


Regards Stepke
Stepke-DSL
 
Posts: 313
Joined: 12. July 2007 16:08
Operating System: Win XP Pro SP3 + SuSe Linux

Re: search a text file for a particular word

Postby ym_chaitu » 04. December 2009 15:47

Stepke-DSL wrote:Try this:

Code: Select all
$count      =    explode($yourword, $yourstring);

if (count($count) == 2)
    {

    // the word $yourword is in $yourstring (just 1 word)

    }
elseif (count($count) > 2)
    {

    // the word $yourword is in $yourstring (more than 1 word)

    }
else
    {

    // the word $yourword is NOT in $yourstring

    }


Regards Stepke


it is not working..this is the code
Code: Select all
<?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.';
$section = file_get_contents('./out.txt', NULL, NULL, 1256, 95);
$count      =    explode($value, $section);

if (count($count) == 2)
    {

    echo "1";

    }
elseif (count($count) > 2)
    {

    echo "2";

    }
else
    {

    echo "3";

    }
/*echo $section;
$fh1=fopen("out_1.txt",'w');
fwrite($fh1, $section);
fclose($fh1);
var_dump($section);*/
fclose($fh);
curl_close($ch);
/*if($value!=$section)
{
   echo "oops";
}
else
{
   echo "Done";
}*/
?>
--
Chaitanya Babu Yanamadala
www.drvirusindia.co.cc
User avatar
ym_chaitu
 
Posts: 33
Joined: 13. November 2009 08:26
Location: Bangalore,India

Re: search a text file for a particular word

Postby Wiedmann » 04. December 2009 15:50

Code: Select all
$value='img alt="[Valid RSS]" title="Valid RSS" src="images/valid-rss.png" /> This is a valid RSS feed.';

the required single instance of word i require is $value,

It doesn't looks like $value is a (one) "word".

BTW:
which will scan the entire txt file and will check for the $value and if it is there then it should display ok

But I don't think you have understand my answer to your last question, how to validate a rss and get the result.
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: search a text file for a particular word

Postby Stepke-DSL » 04. December 2009 15:52

What say
Code: Select all
echo count($count);

?
Stepke-DSL
 
Posts: 313
Joined: 12. July 2007 16:08
Operating System: Win XP Pro SP3 + SuSe Linux

Re: search a text file for a particular word

Postby ym_chaitu » 04. December 2009 15:57

Wiedmann wrote:
Code: Select all
$value='img alt="[Valid RSS]" title="Valid RSS" src="images/valid-rss.png" /> This is a valid RSS feed.';

the required single instance of word i require is $value,

It doesn't looks like $value is a (one) "word".

BTW:
which will scan the entire txt file and will check for the $value and if it is there then it should display ok

But I don't think you have understand my answer to your last question, how to validate a rss and get the result.



no it not a single word that i need to validate with

basically this is my intention

if the out.txt file has these words any where then the feed is a valid rss url or else if not then i will display a message..
--
Chaitanya Babu Yanamadala
www.drvirusindia.co.cc
User avatar
ym_chaitu
 
Posts: 33
Joined: 13. November 2009 08:26
Location: Bangalore,India

Re: search a text file for a particular word

Postby ym_chaitu » 04. December 2009 15:58

Stepke-DSL wrote:What say
Code: Select all
echo count($count);

?


it displays as
Array
--
Chaitanya Babu Yanamadala
www.drvirusindia.co.cc
User avatar
ym_chaitu
 
Posts: 33
Joined: 13. November 2009 08:26
Location: Bangalore,India


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 141 guests