Jump to content

Archived

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

joel2k8

Search for string in a text file..

Recommended Posts

Hello,

 

Okay for some reason i cannot do this, when i open a file in php and search for string, it doesn't seem to work, some codes i tried:

 

$filename = 'example.txt';
$searchfor = 'hello';
$fh = fopen($filename, 'r');
$olddata = fread($fh, filesize($filename));
if(strpos($olddata, $searchfor)) {
           //fount it
}
else {
          //can't find it
}
fclose($fh);

(above it just sample text, i was hoping you could give me sample code that will work)

 

I tried many others like strstr, stristr, preg_replace etc etc, doesn't seem to find it.. this is my text file:

 

sdfsdfsdfsdfsdf[b]hello[/b]sdvsdf
dfadfsdfsdfsddfsdf
sdfsdfsdfs
sdffffffffffffffffffffdv
vsvdsdsf

 

I don't want to find it by line, i want to simply open the text file and find the string..

 

thanks..

Share this post


Link to post
Share on other sites

Do you just want to see if the string exists in the file?  If so, you would use something like:

$filename = 'example.txt';
$searchfor = 'hello';
$file = file_get_contents($filename);
if(strpos($file, $searchfor)) 
{
   echo "String found";
}
?> 

Share this post


Link to post
Share on other sites

<?php

$data = file_get_contents('file.txt');

if(strpos($data, 'findThis') !== FALSE)
{
    // found 'findThis'
}
else
{
    // Did not find 'findThis'
}

?>

 

I see Maq already posted almost identical code now that I clicked post.. But use !== FALSE because strpos could return 0 if the string found starts at offset 0 and that will result in a false in the if statement and would therefore give wrong result:

 

if(0 == TRUE) // FALSE

Share this post


Link to post
Share on other sites

Yes, i just want to see the string exist and if does do something.. though the code you gave me doesnt seem to work, but it is a php file that i am getting the information from:

 

test.php

<a href="http://google.com/">Google</a>
bhjb
njkm,
knklmnnkl
hello
<a href="#">test</a> <a href="http://google.com/">Google</a>
<a href="http://google.com/">Google</a>
<a href="http://google.com/">Google</a>

 

then i try and check to if it exists by using:

 

$old = 'hello';
$filename = 'test.php';
$file = file_get_contents($filename);
if(strpos($file, $old)) {
//string exists
}

 

it doesn't seem to work, does it have to be a txt file? i can write to the php file but can't seem to find the string..

 

thanks

Share this post


Link to post
Share on other sites

Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working..

 

thanks for helping anyway!  ;)

Share this post


Link to post
Share on other sites

Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working..

 

thanks for helping anyway!  ;)

If you still can't get it to work, posting example data would help.

Share this post


Link to post
Share on other sites

Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working..

 

thanks for helping anyway!  ;)

 

I got it to work, before the code was:

 

strpos($filecont, $name)

 

I changed to:

 

strpos($filecont, "$name")

 

Now it works how it should, but for some reason i would of thought it would work with out the " ".

 

Nevermind, thanks for the help, problem is now soved!

 

If you still can't get it to work, posting example data would help.

Share this post


Link to post
Share on other sites

So this is solved?

Share this post


Link to post
Share on other sites

Yeah.. the problem is solved, as i said in my last reply, i accidently put my text in the quote.

 

thanks again for the help.

Share this post


Link to post
Share on other sites

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