Plain text link to HTML with a PHP function and regular expressions

If you have ever wanted to turn a plain text link, http://www.smjdesign.com, into a "linked" HTML version, http://www.smjdesign.com, you can now. Pass the following function your entire text field and it will find all your links and return tagged HTML.

function replace_plain_text_link($plain_text) {
$url_html = preg_replace(
'/(?<!S)((http(s?):\/\/)|(www.))+([\w.1-9\&=#?\-~%;\/]+)/',
'<a href="http$3://$4$5">http$3://$4$5</a>', $plain_text);
return ($url_html);
}
echo replace_plain_text_link("hi this is dummy text before
http://www.smjdesign.com hi this is dummy text after")

The majority of the above regular expression was not created by me. I however cannot locate the original programmer. It was posted on a message board I visited along with ftp and mailto options. If you can help, please comment.

If you have found this post valuable, please share it with others: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Facebook
  • del.icio.us
  • Google
  • StumbleUpon
  • TwitThis

A note about the author

Photograph of Stephen JamesMedia designer with experience in interactive, print and video. I have a marketing and advertising agency background and currently work full-time at a full-service agency. Visit my contact page, even if you would just like to say hello. To stay in touch, follow me on Twitter or connect on a variety of other social networks. Thanks for stopping by!

9 Responses to “Plain text link to HTML with a PHP function and regular expressions”

  1. Michael Says:

    The script works good unless you have a longer url like http://www.myspace.com/wearemongrel than only http://www.myspace.com will be linked and /wearemongrel is left out in the cold

  2. Stephen James Says:

    @Michael It’s fixed now. Your example URL will now work. Thanks.

  3. Matthew Hall Says:

    Great function, thanks. Saved me a bit of time messing about with regular expressions!

    Matthew

  4. Mamsaac Says:

    Works, but not for every URL. For example, Google Maps links =) You would have to add a comma (and some other characters). Still found this useful, thanks.

  5. Michael Says:

    Script still works great for me but now I wonder if that would work to having a mail addy in the text ei a function that makes a mailto-link

  6. Stephen james Says:

    @Michael I try not to have mailto links for anti-spam reasons–and if I do need a mail to link, I try to obfuscate it via JavaScript.

  7. Michael Says:

    Well Stephen I see your point but I wasn´t very specific of my needs as said before your script is excellent if there is a web-link but what if there is a mail-link ei man@abc.com then I would like the mail-link to show up as a mailto-link and unfortunately I can´t make a regular html-mailto in the text since it is php.
    Thanks in advance

  8. Michael Says:

    Btw just like it does above ;)

  9. Stephen James Says:

    @Michael Look into this function in WordPress

    http://codex.wordpress.org/Function_Reference/make_clickable

    It’s inside wp-includes/formatting.php.

Leave a Reply


My Sites