Skip to main content

Replace URLs in text with HTML links



Here is a design though: For example is I put a link such as







http://example.com







in textarea . How do I get PHP to detect it’s a http:// link and then print it as







print "<a href='htttp://example.com'>http://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



Source: Tips4all

Comments

  1. You can also parse the comment text looking for valid urls with preg_match and an adequate regular expression.

    ReplyDelete

Post a Comment