We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

jaytee on 04/09/08


Tagged

regex url pregreplace


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

Folio
mitchkramez


Replace a URL with its domain name and create link


Published in: PHP 


Worst title ever. Basically, this takes a string like: "Check this out: http://snipplr.com/view/5759/replace-a-url-its-domain-name-and-create-link/" and replaces it with "Check this out: snipplr.com [with a link to the full URL]"

  1. preg_replace("/http:\/\/([^\/]+)[^\s]*/", "<a href='$0'>$1</a>", $text);

Report this snippet 

You need to login to post a comment.