$plainTextString = 'Link example with slash-escaped https - https:\/\/www.keskkonnaamet.ee\/sites\/default\/files\/lo_maahoolduse_sp_2016_uus_7.docx <br>Link example with nonescaped http - http://www.tere.eu <br>Link example without protocol - www.tere.eu';
$regexURL = '@((http(s):\/\/)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';
//Turn plain text urls into links
$hyperlinkedString = preg_replace($regexURL, '<a href="//$0">$1</a>', stripslashes($plainTextString));
//Remove http, https protocols
$hyperlinkedString = preg_replace('/(http(s)?:\/\/)*/','',$hyperlinkedString);
echo 'Initial string:<br>'.$plainTextString.'<br><br>Hyperlinked:<br>'.$hyperlinkedString;
Initial string:
Link example with slash-escaped https - https:\/\/www.keskkonnaamet.ee\/sites\/default\/files\/lo_maahoolduse_sp_2016_uus_7.docx
Link example with nonescaped http - http://www.tere.eu
Link example without protocol - www.tere.eu
Hyperlinked:
<small>(URL hovers are identical to textual links - except in cacher ofc)</small>
Link example with slash-escaped https - [www.keskkonnaamet.ee/sites/default/files/lo_maahoolduse_sp_2016_uus_7.docx](www.keskkonnaamet.ee/sites/default/files/lo_maahoolduse_sp_2016_uus_7.docx)
Link example with nonescaped http - www.tere.eu
Link example without protocol - www.tere.eu