Find and replace html links with PHP

Here is a great function that I’m using in a handy string utility class. It takes a string of text (plain text or html) and finds any web addresses ie and turns them into html links (<a> tags).

function parseLinks($str) {
	return ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $str);

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>