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?-??-?()[email protected]:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $value );

Related Articles

Leave a Reply

Your email address will not be published.