I have a custom edit user interface where I allow the user to enter their own URL’s, and so far I have the regex to find the URL’s and turn them all into clickable html links. But I’d also like to give the user the option to enter their own link title, similar to the format here on StackOverflow:
[Name of Link](http://www.yourlink.com/)
How would I alter the code below to extract the title from the brackets, the URL from the parenthesis, AND also turn a regular URL into a clickable link (even if they just enter http://www.yourlink.com/ without a title)?
$text = preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1" target="_blank">\\1</a>', $text);
$text = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
$text = preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i',
'<a href="mailto:\\1">\\1</a>', $text);
Firstly you have to process these links with description, like this:
But now, regular URLs placed in href will match in next replace iteration for regular links, so we need to modify that to exclude it, for example match only when it is not preceded with
":