Reputation: 809
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);
Upvotes: 2
Views: 3416
Reputation: 29462
Firstly you have to process these links with description, like this:
$text = preg_replace(
'/\[([^\]]+)\]\((((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)\)/i',
'<a href="\\2" target="_blank">\\1</a>',
$text
);
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 "
:
$text = preg_replace(
'/(^|[^"])(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'\\1<a href="\\2" target="_blank">\\2</a>',
$text
);
Upvotes: 4
Reputation: 1114
try this :
<?php
$text = "hello http://example.com sample
[Name of Link](http://www.yourlink.com/)
[Name of a](http://www.world.com/)
[Name of Link](http://www.hello.com/)
<a href=\"http://stackoverflow.com\">hello world</a>
<a href='http://php.net'>php</a>
";
echo nl2br(make_clickable($text));
function make_clickable($text) {
$text = preg_replace_callback(
'#\[(.+)\]\((\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|)/))\)#',
create_function(
'$matches',
'return "<a href=\'{$matches[2]}\'>{$matches[1]}</a>";'
),
$text
);
$text = preg_replace_callback('#(?<!href\=[\'"])(https?|ftp|file)://[-A-Za-z0-9+&@\#/%()?=~_|$!:,.;]*[-A-Za-z0-9+&@\#/%()=~_|$]#', create_function(
'$matches',
'return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";'
), $text);
return $text;
}
written (edited) based on following links :
Best way to make links clickable in block of text
Make links clickable with regex
and ...
Upvotes: 1