我有很多链接:
http://exmpale.com/abc/exmpale.html
http://exmpale.com/abc/exmpale1.html
http://exmpale.com/abc/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale1.html
http://exmpale.com/abc/exmpale/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale3.html
http://exmpale.com/abcd/exmpale1.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale4.html
http://exmpale.com/abc/abc/exmpale1.html
http://exmpale.com/abc/abc/exmpale2.html
http://exmpale.com/abc/abc/exmpale3.html
http://exmpale.com/abc/abc/exmpale4.html
http://exmpale.com/abc/abc/exmpale5.html
我使用php,我希望得到像http://exmpale.com/abc/(*).html
这样的所有链接并将其列在数组中。
对不起,我忘记了代码。我的代码
$a = array(
0 => 'http://exmpale.com/abc/exmpale.html',
1 => 'http://exmpale.com/abc/exmpale1.html',
2 => 'http://exmpale.com/abc/exmpale2.html',
3 => 'http://exmpale.com/abc/exmpale/exmpale1.html',
4 => 'http://exmpale.com/abc/exmpale/exmpale2.html',
5 => 'http://exmpale.com/abc/exmpale/exmpale3.html',
6 => 'http://exmpale.com/abcd/exmpale1.html',
7 => 'http://exmpale.com/abcd/exmpale2.html',
8 => 'http://exmpale.com/abcd/exmpale2.html',
9 => 'http://exmpale.com/abcd/exmpale4.html',
10 => 'http://exmpale.com/abc/abc/exmpale1.html',
11 => 'http://exmpale.com/abc/abc/exmpale2.html',
12 => 'http://exmpale.com/abc/abc/exmpale3.html',
13 => 'http://exmpale.com/abc/abc/exmpale4.html',
14 => 'http://exmpale.com/abc/abc/exmpale5.html',
);
foreach($a as $k => $v ){
$abc = preg_match_all('/http\:\/\/exmpale\.com\/abc\/(.*?)\.html/',$v,$b);
print_r($b[1]);
}
我希望得到结果。
$result = array(
'http://exmpale.com/abc/exmpale.html',
'http://exmpale.com/abc/exmpale1.html',
'http://exmpale.com/abc/exmpale2.html');
但它取得了所有结果,请帮助我 谢谢你。
答案 0 :(得分:1)
只需使用php函数preg_match_all,匹配的字符串将加载到您作为参数提供给上述函数的数组中。请阅读preg_match_all上的PHP手册,并根据需要创建一个正则表达式。您可以测试正则表达式here
答案 1 :(得分:0)
由于您没有提供存储URL的数据结构,我认为它是一个原始字符串:
$data_raw = "http://exmpale.com/abc/exmpale.html
http://exmpale.com/abc/exmpale1.html
http://exmpale.com/abc/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale1.html
http://exmpale.com/abc/exmpale/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale3.html
http://exmpale.com/abcd/exmpale1.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale4.html
http://exmpale.com/abc/abc/exmpale1.html
http://exmpale.com/abc/abc/exmpale2.html
http://exmpale.com/abc/abc/exmpale3.html
http://exmpale.com/abc/abc/exmpale4.html
http://exmpale.com/abc/abc/exmpale5.html";
$urls = explode("\n", $data_raw);
$accept_host = "exmpale.com";
$accept_path = "/abc/";
$filtered_urls = array_filter($urls, function($url) use($accept_host, $accept_path) {
$parts = parse_url($url);
return $parts['host'] == $accept_host
&& strpos($parts['path'], $accept_path) === 0
&& strpos($parts['path'], '/', strlen($accept_path)) === false;
});
printf("<pre>%s</pre>", print_r($filtered_urls, true));
收率:
Array
(
[0] => http://exmpale.com/abc/exmpale.html
[1] => http://exmpale.com/abc/exmpale1.html
[2] => http://exmpale.com/abc/exmpale2.html
)