logicK
logicK

Reputation: 207

Find Emails in string and replace section with mailto link

I've done a little bit of looking around but the examples I found weren't working for domains with subdomains that were google or yahoo, etc.

So my domain @nananananananabatman.com isn't getting filtered out... What can I do to ensure this works?

Upvotes: 0

Views: 286

Answers (1)

user11845736
user11845736

Reputation:

So it looks like the issue you might have can be resolved with something like this.

You should look more into learning about preg_replace and what you can do with it. Tutorials even would help give you better ideas how it works and how it can be utilized.

Here is one you can start with: https://www.tutorialspoint.com/php/php_preg_replace

Try this code and let me know if it works.

function auto_email($text){
    $mail_pattern = "/([A-z0-9\._-]+\@[A-z0-9_-]+\.)([A-z0-9\_\-\.]{1,}[A-z])/";
    $text = preg_replace($mail_pattern, '<a href="mailto:$1$2">$1$2</a>', $text);

    return $text;
}

echo auto_email($message);

Upvotes: 3

Related Questions