从数组中获取内部链接和外部链接(RegEx和PHP)

时间:2022-08-01 03:37:07

This is my array output:

这是我的数组输出:

Array
(
    [0] => http://www.example.com/some-page/
    [1] => http://www.example.com/another-page/
    [2] => http://www.example.com/third-page/
    [3] => https://www.example.com/ssl/
    [4] => https://www.example.com/with-slash-at-the-end/
    [5] => https://www.example.com/without-slash-at-the-end
    [6] => /internal-link
    [7] => /anther-internal-link/
    [8] => https://www.my-own-domain.com/internal-link-too-but-with-absolute-path/
)

How can I get only external and only internal links from this array? I doesn't matter which one.

如何从此阵列中仅获取外部链接和内部链接?哪个不重要。

1 个解决方案

#1


1  

This regex seems to work fine for finding internal link

这个正则表达式似乎可以很好地找到内部链接

(.*my-own-domain\.com.*)|(^\/.*$)

Regex Demo

正则表达式演示

PHP Code

PHP代码

$re = "/(?:.*my-own-domain\\.com.*)|(?:^\\/.*$)/m";
foreach($str as $x) {
   if (preg_match($re, $x)) {
      echo $x . "" . "\n";
   }
}

Ideone Demo

Ideone演示

#1


1  

This regex seems to work fine for finding internal link

这个正则表达式似乎可以很好地找到内部链接

(.*my-own-domain\.com.*)|(^\/.*$)

Regex Demo

正则表达式演示

PHP Code

PHP代码

$re = "/(?:.*my-own-domain\\.com.*)|(?:^\\/.*$)/m";
foreach($str as $x) {
   if (preg_match($re, $x)) {
      echo $x . "" . "\n";
   }
}

Ideone Demo

Ideone演示