我需要获得pdf扩展名的链接。我的代码是:
<?php
set_time_limit (0);
curl_setopt($ch, CURLOPT_URL,"http://example.com");
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
preg_match_all( '/<a href="(http:\/\/www.[^0-9].+?)"/', $result, $output, PREG_SET_ORDER); // read all links
foreach($output as $item ){
$n=strlen($item);
$m=$n-3;
$buffer_n=$item;
$buffer_m="";
$buffer_m=$buffer_n[$m].$buffer_n[$m+1].$buffer_n[$m+2];
$ekstension = 'pdf';
if ($buffer_m == $ekstension) {
print_r($item);
echo '<br>';
}
}
?>
代码给我空白结果,我的代码出了什么问题?非常感谢你:))
答案 0 :(得分:0)
使用简单的html dom解决,代码是:
<?php
set_time_limit(0);
include 'simple_html_dom.php';
$url = 'example';
$html = file_get_html($url) or die ('invalid url');
foreach($html->find('a') as $e) {
$link= $e->href;
if (preg_match('/\.pdf$/i', $link)) {
}
}
print_r($result);
?>