Jump to content

ereg(); with files?


JP128

Recommended Posts

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.....?
Link to comment
Share on other sites

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... :(
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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:

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

[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]
Link to comment
Share on other sites

[code]<?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);
?>[/code]


That is a sample code
Link to comment
Share on other sites

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

[code]<?php

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

$data = file_get_contents ($filename);

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

?> [/code]
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.