I would like to detect if a URI is located in a string, properly sanitize it, and output it with the proper anchor tags.
ie, a user inputs:
Check out our profile on facebook!
https://facebook.com/ourprofile
and our twitter!
twitter.com/#!/ourprofile
and email us!
ourprofile@stack.com
Is there a way to determine that there are URIs located in the string, sanitize unsafe characters and properly output a safe anchor?
so the output would be:
Check out our profile on facebook!
<a href="https://www.facebook.com/ourprofile">https://www.facebook.com/ourprofile</a>
and our twitter!
<a href="http://www.twitter.com/#!/ourprofile">twitter.com/#!/ourprofile</a>
and email us!
<a href="mailto:ourprofile@stack.com">ourprofile@stack.com</a>
the ideas i had in mind were using preg_match
and simple preg_replace
for removing unsafe characters, but this has failed me and im just going in circles, i dont really know where to start for this, as im almost certain a blacklist approach such as that is not proper or secure.