我有这个字符串:
http://sh.st/st/8052f1bbc624add41cd2a6236f76cfc8/https://www.rapidvideo.com/embed/o1cTaZbux
我想将网址与此分开提取。
我的代码:
$pattern = '@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@';
$str= "http://sh.st/st/8052f1bbc624add41cd2a6236f76cfc8/https://www.rapidvideo.com/embed/o1cTaZbux";
if($num_found = preg_match_all($pattern, $str, $out))
{
echo "FOUND ".$num_found." LINKS:\n";
print_r($out[0]);
}
但输出错误:
Array
(
[0] => http://sh.st/st/8052f1bbc624add41cd2a6236f76cfc8/https
[1] => www.rapidvideo.com/embed/o1cTaZbux
)
我想输出:
Array
(
[0] => http://sh.st/st/8052f1bbc624add41cd2a6236f76cfc8
[1] => https://www.rapidvideo.com/embed/o1cTaZbux
)
请帮忙
答案 0 :(得分:1)
也许没有语句等乐趣,并通过函数preg_split来完成?
EG。
$urls = preg_split('/ (http|https) /', $str, PREG_SPLIT_DELIM_CAPTURE);
修改强>
要执行此操作,您必须添加新标记+相互添加结果。
$urls = preg_split('/(http|https)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ($urls as $k => $v) {
if ($k % 2 != 0) {
continue;
}
$list[] = $urls[$k].$urls[$k+1];
}
答案 1 :(得分:1)
$str= "http://sh.st/st/8052f1bbc624add41cd2a6236f76cfc8/https://www.rapidvideo.com/embed/o1cTaZbux";
$foo = preg_split('#(http://)|(https://)#', $str,0,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < sizeof($foo); $i += 2) {
$delim = isset($foo[$i + 1]) ? $foo[$i + 1] : '';
$tmp[] = $foo[$i] . $delim;
}
print_r($tmp);
阵列(
[0] =&gt; http://sh.st/st/8052f1bbc624add41cd2a6236f76cfc8/
[1] =&gt; https://www.rapidvideo.com/embed/o1cTaZbux
)