Posts Tagged ‘regular expressions’

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

Wednesday, May 7th, 2008

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")

(more...)


My Sites

Books Now Reading

  • Unfortunately, Amazon Web Services (AWS) has disabled wishlist requests. Please visit their site to view my Amazon Reading List.