While researching regex info online, I found this wonderful php function:
function convert($input) {
$pattern = '@(http(s)?://)?(([a-zA-Z0-9])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
return $output = preg_replace($pattern, '<a href="http$2://$3">$0</a>', $input);
}
I was posted here on SO by Gero Nikolov in THIS POST
It converts text links to clickable links, as many as you have in a paragraph, spot on, it works great.
However, and here's my question, when this code encounters an email address, it omits the name before the email's domain and also the @ like so: name@domain.com
I'm guessing the return
part of the function would have to be amended to include the email link, but I'm not certain as to how this would be integrated into the above function. Would a conditional statement be necessary? Regex is still new to me so I'm not sure if this would be the right direction to pursue.
Then it gets weird when a link is at the end of a sentence and that sentence is wrapped in single or double quotes (as in a press release or article). What happens there is that the end punctuation (period, exclamation, question mark, etc.) is included in the link along with the closing quote, like this: "Make sure to check out their website at domain.com."
If the period is removed, the closing quote is still included in the link: "Make sure to check out their website at domain.com"
Things get even weirder when the last word of a regular sentence (not including any link text) lies within single or double quotes. For example, the last word with its punctuation and the closing quote are both turned into a link as such: "This is the first sentence. This is the second sentence. And this is the last sentence."
If the closing punctuation (period, etc.) is removed, no link is created and the closing quote is unaffected: "This is the first sentence. This is the second sentence. And this is the last sentence"
So again, the question is, how should this function be modified to handle email addresses and also to stop adding ending punctuation and closing quotes if a link is at the end of a quoted sentence? Additionally, how can it be modified to prevent turning the last word with its ending punctuation into a link when it is in a quoted sentence?
I'm still new to regex work so any help, links, nudges in the right direction(s) are appreciated. There's one further thing that this function does, it makes links out of phone numbers separated by dots (as in 555.555.5555 gets turned into 555.555.5555
) but I think that problem can wait for now. Again, I'm not asking for a rewritten code snippet, but some direction would be greatly appreciated as I have no idea where to begin modifying this. Thanks!