regex to find valid URLS and make clickable HTML link

The following PHP preg_replace function will looking for valid URL’s in a string and turn them into clickable links.

For example, if you have a block of text and need to find all URL’s and turn into clickable links.

Note: replace target=”_blank”) to have links open in the same window.

$value = preg_replace( '!(((f|ht)tp(s)?://)[-a-zA-Z?-??-?()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $value );

Was this article helpful?

Related Articles