author: Krasimir Tsonev

Hi there, I'm . Senior front-end engineer with over 13 years of experience. I write, speak and occasionally code stuff. Follow me on Twitter, GitHub, Facebook or LinkedIn

PHP: convert emails to mailto links

It looks like an easy task, but I decided to share it.

I think that preg_replace function is the best option for doing that. It's just one line of code:

preg_replace('/([a-zA-Z0-9_\\-\\.]*@\\\\S+\\\\.\\\\w+)/', '<a href="mailto:$1">$1</a>', $str);
If you enjoy this post, share it on Twitter, Facebook or LinkedIn. Or maybe comment below: