Jump to content

Archived

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

JP128

ereg(); with files?

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

Share this post


Link to post
Share on other sites
hello JP128. Could you be more specific as to what you need to use ereg() for.

Share this post


Link to post
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... :(

Share this post


Link to post
Share on other sites
$data = file_get_contents('file');

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

Share this post


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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
here is a sample code...

[a href=\"http://pastebin.com/708134\" target=\"_blank\"]http://pastebin.com/708134[/a]

Share this post


Link to post
Share on other sites
If you want it to be case-insensitive, you might consider using eregi() or stripos()

Share this post


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

Share this post


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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Thanks... Lol, After I did that it worked. Now I can look more into that stuff.

Share this post


Link to post
Share on other sites

×

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.