Here is a design though: For example is I put a link such as
in textarea. How do I get PHP to detect it’s a http://
link and then print it as
print "<a href='http://www.example.com'>http://www.example.com</a>";
I remember doing something like this before however, it was not fool proof it kept breaking for complex links.
Another good idea would be if you have a link such as
http://example.com/test.php?val1=bla&val2blablabla%20bla%20bla.bl
fix it so it does
print "<a href='http://example.com/test.php?val1=bla&val2=bla%20bla%20bla.bla'>";
print "http://example.com/test.php";
print "</a>";
This one is just an after thought.. stackoverflow could also probably use this as well :D
Any Ideas
This worked for me (turned one of the answers into a PHP function)
If you want to trust the IANA you can get your current list of offcially supported TLDs in use there like:
Makes Søren Løvborg's solution #2 a bit less verbose and spares you the hassle of updating the list, nowadays new tlds are thrown out so carelessly ;)
Let's look at the requirements. You have some user-supplied plain text, which you want to display with hyperlinked URLs.
Edit: Check out Bitbucket for the latest version, with support for email addresses, authenticated URLs, URLs in quotes and parentheses, HTML input, as well as an updated TLD list.
Please report bugs and enhancement requests using the Bitbucket issue tracker. They're easier to keep track of that way (and don't clutter the comment area).
Here's my take:
Edit: The following code fixes the above two problems, but is quite a bit more verbose since I'm more or less re-implementing
preg_replace_callback
usingpreg_match
.Here is the code using Regular Expressions in function
This RegEx should match any link except for these new 3+ character toplevel domains...
It's not written by me, I'm not quite sure where I got it from, sorry that I can give no credit...
This should get your twitter handle without touching on your email /(?<=^|(?<=[^a-zA-Z0-9-.]))@([A-Za-z]+[A-Za-z0-9]+)/i