Jump to content

Search for string in a text file..


joel2k8

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

Link to comment
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";
}
?> 

Link to comment
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

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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