View Full Version : Convert email address in string to html

08-05-2008, 06:19 AM
Hey there:

Client has written email addresses into a mysql text field. I'd like to have it output to the page so that it shows up as a valid mailto link - now it just spits out the html characters, but browser doesn't recognize as a link. For example, text field contains <a href = "mailto:asl@vassar.edu">asl@vassar.edu</a>. As well, they have other html formatting in the sql text field, like <em>, etc. How can I output this to the page and have that html formatting preserved?

Thanks very much.

08-05-2008, 07:39 AM
I think this is what you're asking...

echo '<a href="'.$data['email'].'">'.$data['email'].'</a>';

08-05-2008, 08:25 AM
You can also use following method to get what you want:

echo "<a href=\"mailto:".$row['user_email']."\">".$row['user_email']."</a>";

08-05-2008, 10:15 AM
Personally I like using (s)printf where available. It tends to make it really easy to add additional fields for display. I also like to use double quotations around any attributes, so I'm always escaping data:

printf("<a href=\"mailto:%s\">%s</a>\n", $row['user_email'], $row['user_email']);

I guess thats what C has done to me.

08-06-2008, 02:17 AM

Thanks for the replies. Reading my original entry, I was totally unclear though. Basically, the users have written a block of text - a journal description - into a longtext field. So there is a paragraph of text. Inside the paragraph, they have entered in email addresses, surrounded by html formatting:

"this is a great journal. If you'd like to email the author, you can at <a href='mailto:donhoe@don.com'>donhoe@don.com</a>. Then he'll respond with a thoughful <em>card</em> and send your wife flowers."

So, I'd like to output the paragraph, and have the html pieces actually display as html.

Thanks for any help.

08-06-2008, 04:59 AM
This depends on exactly what you want to do, and how the data was inserted. If printing it out directly from the field to the browser keeps the actual characters, it is keeping their conversion counterparts (&amp; &gt; &lt; etc). If you don't need to do any serious matching a simple htmlspecialchars_decode() should convert them back into actual html tags. Be warned though that if there is any other html data it will also be parsed, including any non-empty tags treated as empty (like an </table> without a start <table>). This is why we use things like bbcode tags to take care of special conversions for us without risking format html breakage.