$re = '/^(?:(?:https?)(?::\/\/)?)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z0-9\x{00a1}-\x{ffff}\-]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/ium';
$str = 'https://www.google.com
http://www.google.com
http://google.com/
https://google.com/
www.google.com
google.com
https://www.google.com.ua
http://www.google.com.ua
http://google.com.ua
https://google.com.ua/
www.google.com.ua
google.com.ua
https://mail.google.com
http://mail.google.com
mail.google.com
http://xn--j1ail.xn--p1ai/
http://xn--j1ail.xn--p1ai/
https://кот.рф
https://кот.рф/кто-там?троль=0&ролф=9
https://www.google.com/search?sxsrf=ALeKk03dlc_OUT_SIhGbPkeGLoL30Dp-2w%3A1614676106917&ei=igA-YImTL83MaL_3nMgM&q=linkify+php&oq=linkify+php&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyCAgAEBYQHhATMggIABAWEB4QEzoHCAAQsAMQQzoCCAA6BAgAEEM6CAgAEBYQChAeOgYIABAWEB5QpixY5UNgjkZoA3ACeACAAcYBiAGhBpIBAzIuNZgBAKABAaoBB2d3cy13aXrIAQrAAQE&sclient=gws-wiz&ved=0ahUKEwiJgJaeoZHvAhVNJhoKHb87B8kQ4dUDCA0&uact=5
http://google
https://google.c
google
google.
.google
.google.com
goole.c
...
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php