I'm trying to preg_replace to detect a link in a string. Example below
$descriptionlink = "This is a test www.google.com";
$descriptionlink = preg_replace('/.*?\b((?:(?:https?|ftp|file):\/\/|www\.|ftp\.)?[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]\.[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]).*?/i', '$1', $descriptionlink);
It works great if there is a link in the string and echos out only the link like I want it. Now if the string is just text it will echo out the text. If it doesn't have a link I don't want to see the text in the string.
example of outputs I want
input - This is a test www.google.com
output - www.google.com
That above works great but it will do this and echo out the text below if no link
input - This is a test
output - This is a test
I want the output to be blank if it's just text.
Thanks
$descriptionlink = preg_replace('/.*?((?:(?:https?):\/\/|www\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]).*?|.*/i', '$1', $descriptionlink);
this... will replace all except the link...
$descriptionlink = preg_replace('/.*?\b((?:(?:https?|ftp|file):\/\/|www\.|ftp\.)?[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]\.[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]).*?/i', '$1', $descriptionlink);