假设我有一个像这样的字符串:
$urlsString = "http://foo.com/barhttps://bar.com//foo.com/foo/bar"
我想得到一个像这样的数组:
array(
[0] => "http://foo.com/bar",
[1] => "https://bar.com",
[0] => "//foo.com/foo/bar"
);
我正在寻找类似的东西:
preg_split("~((https?:)?//)~", $urlsString, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
其中 PREG_SPLIT_DELIM_CAPTURE 定义是:
如果设置了此标志,分隔符模式中的括号表达式也将被捕获并返回。
也就是说,上面的 preg_split
返回:
array (size=3)
0 => string '' (length=0)
1 => string 'foo.com/bar' (length=11)
2 => string 'bar.com//foo.com/foo/bar' (length=24)
知道我做错了什么或有其他想法吗?
PS:我使用的是this regex直到我意识到它不涵盖这种情况。
编辑:
正如 @sidyll 指出的那样,我缺少 preg_split
参数中的 $limit
。不管怎样,我的正则表达式有问题,所以我将使用@WiktorStribiżew 建议。
最佳答案
您可以将 preg_match_all
与以下正则表达式一起使用:
'~(?:https?:)?//.*?(?=$|(?:https?:)?//)~'
请参阅regex demo .
详细信息:
(?:https?:)?
-https:
或http:
,可选(1 次或 0 次)//
- 双/
.*?
- 除换行符之外的任何 0 个以上字符,尽可能少到第一个(?=$|(?:https?:)?//)
- 两者之一:$
- 字符串结尾(?:https?:)?//
-https:
或http:
,可选(1 次或 0 次),后跟带有两个/
下面是 PHP demo :
$urlsString = "http://foo.com/barhttps://bar.com//foo.com/foo/bar";
preg_match_all('~(?:https?:)?//.*?(?=$|(?:https?:)?//)~', $urlsString, $urls);
print_r($urls);
// => Array ( [0] => http://foo.com/bar [1] => https://bar.com [2] => //foo.com/foo/bar )
关于php - 从字符串中提取 url 之间不带空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43495572/