My 5th book "50 Tips on JavaScript" is on sale. Go get it from https://50tips.dev.
Illustrated guide to 50 of the most interesting JavaScript topics.

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. To leave a comment go here.