Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting emails in a text

I'm trying to create a function that translates every occurrence of a plain text email address in a given string into it's htmlized version.

Let's say I have the following code, where htmlizeEmails is the function I'm looking for:

$str = "Send me an email to [email protected].";
echo htmlizeEmails($str); // Echoes "Send me an email to <a href="mailto:[email protected]">[email protected]</a>."

If possible, I'd like this function to use the filter_var function to check if the email is valid.

Does anyone know how to do this? Thanks!

Edit:

Thanks for the answers, I used Shocker's regex to match potential email addresses and then, only if the filter_var validates it, it gets replaced.

function htmlizeEmails($text)
    preg_match_all('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', $text, $potentialEmails, PREG_SET_ORDER);
    
    $potentialEmailsCount = count($potentialEmails);
    for ($i = 0; $i < $potentialEmailsCount; $i++) {
        if (filter_var($potentialEmails[$i][0], FILTER_VALIDATE_EMAIL)) {
            $text = str_replace($potentialEmails[$i][0], '<a href="mailto:' . $potentialEmails[$i][0] .'">' . $potentialEmails[$i][0] .'</a>', $text);
        }
    }
}
like image 527
federico-t Avatar asked Mar 19 '12 00:03

federico-t


1 Answers

$str = preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '<a href="mailto:$1">$1</a>', $str);

where ([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}) is the regular expression used for detecting an email address (this is a general example, email addresses may be more complicated than this and not all addresses may be covered, but finding the perfect regex for emails is up to you)

like image 186
QuantumBlack Avatar answered Oct 21 '22 02:10

QuantumBlack