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

时间:2016-04-10 07:41:29

标签: php regex

这是我的数组输出:

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/
)

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

1 个答案:

答案 0 :(得分:1)

这个正则表达式似乎可以很好地找到internal link

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

<强> Regex Demo

PHP代码

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

<强> Ideone Demo