Jump to content


Photo

ereg(); with files?


  • Please log in to reply
11 replies to this topic

#1 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 13 June 2006 - 03:59 AM

Ok, I just started with PHP again, and I was wondering how to do ereg(); with files. If anyone can post a sample code that opens a file, and puts all of the contents into a string, so I would be able to read the contents, that would be awesome. I have tried getting my code to work, but no luck...


thanks, johnny



One more thing, why have I had trouble posting? I sometimes get an Access Denied message.....?
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#2 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 13 June 2006 - 05:50 AM

hello JP128. Could you be more specific as to what you need to use ereg() for.
Given enough eyeballs all bugs are shallow. My Website

#3 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 14 June 2006 - 02:55 AM

I am using ereg() to look at the text of a file just to see if a persons name exists. I need the script to open a file, look at the contents, and see if a name is in there. I am using this as a test to see if a text document can be used as a little database. I have done it before but went away from PHP, and now I am coming back to it. I lost the old script i had... :(
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 03:22 AM

$data = file_get_contents('file');

[a href=\"http://www.php.net/file_get_contents\" target=\"_blank\"]http://www.php.net/file_get_contents[/a]
~ D Kuang

#5 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 14 June 2006 - 04:39 AM

Well, yea, I can get that part. . . But when ever I put the if() statement in, it always comes back false. here is what I use....

if(ereg($name, $data)){
echo "That name was found... ";
}
else {
echo "That name does not exist. "
}


That is what I am using... I put my name in there, Johnny, and i know for a fact it is in there... but when I search the contents with the if statement it still says it doesn't exist.



Thanks, Johnny
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 04:42 AM

For a simple search like that, you should consider using something else, like strpos() or stripos() if you want it to not be case-sensitive:

if (strpos($data, $name) !== false) {
   // name found
} else {
   // name not found
}

[a href=\"http://www.php.net/strpos\" target=\"_blank\"]http://www.php.net/strpos[/a]
[a href=\"http://www.php.net/stripos\" target=\"_blank\"]http://www.php.net/stripos[/a]

~ D Kuang

#7 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 14 June 2006 - 08:50 AM

here is a sample code...

[a href=\"http://pastebin.com/708134\" target=\"_blank\"]http://pastebin.com/708134[/a]
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#8 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 14 June 2006 - 11:12 AM

If you want it to be case-insensitive, you might consider using eregi() or stripos()
Given enough eyeballs all bugs are shallow. My Website

#9 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 14 June 2006 - 06:17 PM

I dont care if it is case sensative... I am trying to get that script to work. It will always come back false. I don't know what I am supposed to do with it....
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#10 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 15 June 2006 - 02:00 AM

<?php 
$name = "johnny"; 
$filename = "test.txt";
$handle = fopen ("test.txt", "r"); //need to remove the spaces ...
$data = file_get_contents ($handle);
if (ereg($name, $handle)){
echo "Yes<br>";
}
else {
echo "No<br>";
}
fclose ($handle);
?>


That is a sample code
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#11 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 15 June 2006 - 02:02 AM

That's wrong, file_get_contents() don't accept a handle as argument.

<?php 

$name = "johnny"; 
$filename = "test.txt";

$data = file_get_contents ($filename);

if (ereg($name, $data)){
   echo "Yes<br>";
} else {
   echo "No<br>";
}

?>

~ D Kuang

#12 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 15 June 2006 - 02:10 AM

Thanks... Lol, After I did that it worked. Now I can look more into that stuff.
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users