JP128 Posted June 13, 2006 Share Posted June 13, 2006 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, johnnyOne more thing, why have I had trouble posting? I sometimes get an Access Denied message.....? Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/ Share on other sites More sharing options...
azuka Posted June 13, 2006 Share Posted June 13, 2006 hello JP128. Could you be more specific as to what you need to use ereg() for. Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-44866 Share on other sites More sharing options...
JP128 Posted June 14, 2006 Author Share Posted June 14, 2006 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 https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45335 Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 $data = file_get_contents('file');[a href=\"http://www.php.net/file_get_contents\" target=\"_blank\"]http://www.php.net/file_get_contents[/a] Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45340 Share on other sites More sharing options...
JP128 Posted June 14, 2006 Author Share Posted June 14, 2006 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 https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45360 Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 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 https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45364 Share on other sites More sharing options...
JP128 Posted June 14, 2006 Author Share Posted June 14, 2006 here is a sample code...[a href=\"http://pastebin.com/708134\" target=\"_blank\"]http://pastebin.com/708134[/a] Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45419 Share on other sites More sharing options...
azuka Posted June 14, 2006 Share Posted June 14, 2006 If you want it to be case-insensitive, you might consider using eregi() or stripos() Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45451 Share on other sites More sharing options...
JP128 Posted June 14, 2006 Author Share Posted June 14, 2006 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.... Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45662 Share on other sites More sharing options...
JP128 Posted June 15, 2006 Author Share Posted June 15, 2006 [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 https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45786 Share on other sites More sharing options...
poirot Posted June 15, 2006 Share Posted June 15, 2006 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 https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45787 Share on other sites More sharing options...
JP128 Posted June 15, 2006 Author Share Posted June 15, 2006 Thanks... Lol, After I did that it worked. Now I can look more into that stuff. Link to comment https://forums.phpfreaks.com/topic/11834-ereg-with-files/#findComment-45788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.