Monday, March 8, 2010

Logic to get Email From a Paragraph

My plan was to get email ids from the page posted data in PHP, it was the requirement of my module at that time to do so I write some code like this
<?php
function get_emails ($str)
{
$emails = array();
preg_match_all("/\b\w+\@\w+[\.\w+]+\b/", $str, $output);
foreach($output[0] as $email) array_push ($emails, strtolower($email));
if (count ($emails) >= 1) return $emails;
else return false;
}

# Here is how to use it.

# Sample string containing email addresses;
$str = "happy happy@gmail.com bingo happy@test.com jojo jojo@badjojo.com";

# Get the emails on arrays;
$emails = get_emails ($str);

# Print that arrays;
print_r ($emails);
?>



Output

Array ( [0] => happy@gmail.com [1] => happy@test.com [2] => jojo@badjojo.com )


Now I have given enough tips try some experimenting lust of yours and utilize it according to your need.

No comments:

Post a Comment