This question has already been answered:

Hello. Problem. There is a variable.

$text = 'В нашей строке присутсвуют следующие ссылки: http://snipcode.ru, ftp://php.net и https://google.com'; 

How to get all references to an array with this variable?

Tried so

 preg_match_all('/<a[^>(href=)]+href="http:\/\/(www\.)?hashcode.ru"[^>]+>([A-Za-zА-Яа-я0-9 ]+)<\/a>/isu', $text, $array); print_r($array); 

As a result, I received an array - Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) )

But something I can’t see from this array what links are there.

I try to look at the link print_r($array[0]); I get Array ( )
Help me figure out php is not strong. What could dig, that I share.

Reported as a duplicate by Grundy , user207618, αλεχολυτ , Denis Bubnov , Crantisz on Mar 28 '17 at 14:15 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • I think you have a mistake in the pattern - tCode
  • There is a small suspicion that you thoughtlessly copied the decision from the old question to SO. - user207618
  • @Other Yeah hashcode.ru if I thought that, I would not ask a question. And so that in Google everyone feeds from text links to make clabbel. And I just need to collect them in one place. - Anatoly
  • there is no hashcode.ru entry in your line) - tCode
  • one
    @Grundy is not there, the link should start with www or // but I don’t need it, just find all the links in the text and collect them in one array. That's all. - Anatoly

2 answers 2

With the assumption that only commas and whitespace can separate links:

 $text = 'В нашей строке присутсвуют следующие ссылки: http://snipcode.ru, ftp://php.net и https://google.com'; preg_match_all('#(?:https?|ftp)://[^\s\,]+#i', $text, $matches); var_dump($matches); 

Result:

 array(1) { [0]=> array(3) { [0]=> string(18) "http://snipcode.ru" [1]=> string(13) "ftp://php.net" [2]=> string(18) "https://google.com" } } 
     $text = 'В нашей строке присутсвуют следующие ссылки: http://snipcode.ru, ftp://php.net и https://google.com'; preg_match_all("_(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!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]))|(?:(?:[az\x{00a1}-\x{ffff}0-9]+-?)*[az\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[az\x{00a1}-\x{ffff}0-9]+-?)*[az\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[az\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?_iuS", $text, $urls); var_dump($urls); 

    https://repl.it/GfwT/0

    • http://example.org?id=5 - takes only the domain. http://example.org/?id=5, ... catches a comma, although the author uses commas to separate the list. - vp_arth