champrock Posted February 6, 2008 Share Posted February 6, 2008 hi i have a small text file containing the details of my users (it was very old when i did not use databases) So i want to create something in php which will open that details.txt file and search for email addresses and lists them together with the name of the person. The name of the person can come easily from the text file as the files are names john.txt, tim.txt etc. I just need to extract emails now. Can anyone help me? ALso, it should list all email addresses (sometimes there are 2-3 email addresses) Thanks Quote Link to comment Share on other sites More sharing options...
frijole Posted February 6, 2008 Share Posted February 6, 2008 if the amount of text files you have are small enough to have them named by first names you should probably just enter them into a real DB. Quote Link to comment Share on other sites More sharing options...
champrock Posted February 6, 2008 Author Share Posted February 6, 2008 text files are small (80-90lines) but the number of text files is large. (more than 1500 unique files) so i need some script to extract that data which i can add to a data properly. i dont want to add raw data to the database Quote Link to comment Share on other sites More sharing options...
champrock Posted February 7, 2008 Author Share Posted February 7, 2008 can anyone help me out? i just need to know the exact function to extract email addresses. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 the function you're after is extract_this_email_address_thingy_from_my_custom_file_format() hope that helps... Quote Link to comment Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 Don't believe me? oh ok, then lets do some legwork: steps: 1) open file 2) parse file line by line 3) parse line to extract email address What format are your text files in? e.g. name|email , or name,email , or "name"|"email" This information will get your started. Next thing to do is learn how to open a file in PHP hint: http://www.php.net/fopen Quote Link to comment Share on other sites More sharing options...
champrock Posted February 7, 2008 Author Share Posted February 7, 2008 Details for customer first name, last name email address -- example@example.com address pin this is my text file. i just need to know how to extract example@example.com from that file. moreover, i have opened the file already and parsed it but i dont know how to extract the email. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 Woah, so you've got 1 file per customer... ? What is your line separator? \r\n or just \n ? Something like this should do: $handle = fopen("filename.txt","r"); $count = 1; while($line = fgets($handle,4096) !== false){ if($count == 3){ $email = $line; echo $email; } $count++; } I'm just guessing here that the email address is ALWAYS on the third line. And this will just echo out the email address. Anything else you want you'll have to work yourself Quote Link to comment Share on other sites More sharing options...
champrock Posted February 7, 2008 Author Share Posted February 7, 2008 thanks a lot. that is the problem that i dont have email always on the third line. therefore i want something "preg_match" which can probably search the text file for occurances of "@" character and then echoing the string before and after that (to get the complete email) Quote Link to comment Share on other sites More sharing options...
champrock Posted February 7, 2008 Author Share Posted February 7, 2008 preg_match("/^[\ a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i", $info, $matches); does not work :( Quote Link to comment Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 I always find working from the bottom up with regular expressions works best. example: // Test string $str = "test@email.com"; // Regular expression. $regex = '/^[\ a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i'; // Test.... if(preg_match($regex,$str)){ echo "it worked"; } I find using the above test harness helps me iron out any regular expressions i'm trying out. Run it through your ide with different regular expressions as a separate script. Allows for quick easy building of regex. Quote Link to comment Share on other sites More sharing options...
stlewis Posted February 7, 2008 Share Posted February 7, 2008 You're on the right track as far as looking at regular expressions, but aschk has point...regex can be kind of tricky, so its best to "put together" a regex piece by piece, comparing it against a sample file. If you want a somewhat smoother method of checking your regular expressions, there's plenty of software out there that will allow you to test your regex against a sample, just do a quick search on Google. The regex I use in the following example may not work for you...if not, reverse engineer it a little bit, (paying attention to any line feeds in your file...those can mess with regular expression matching), and see what you come up with. <?php $contents = file_get_contents(customer_file.txt); $regex = '/^[a-z0-9.-_]+@[a-z0-9.-_]+\.[a-z]{2,4}$/'; $eval = preg_match($regex, $contents, $matches); if ($eval) { echo "The email in this file is ".$matches[0]; } ?> The key to the whole thing here is the $matches variable, that you pass as a third argument to preg_match. $matches is an array that holds any matches (go figure), to your test pattern. I'm not 100% sure on this, but I believe that your first complete match is found in $matches[0], that'll be the email you're looking for. Hope this helps! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.