Jump to content

Simple php code. Confused


champrock

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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!

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.