KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 bob@example.com."; echo htmlizeEmails($str); // Echoes "Send me an email to <a href="mailto:bob@example.com">bob@example.com</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); } } }
Tags (comma-separated)
Save Edits
Cancel