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.

With over two decades of deep front-end expertise, I offer comprehensive web consultancy and stack audits, alongside specialized workshops, training, and engaging public speaking to elevate your team's skills and optimize your digital presence. Contact me.