0

我想使用正则表达式来识别 .pdf 文件名中的空格

到目前为止,我已经能够识别到文件的 src 链接,但它无法识别文件名中的空格。

   <?php
   echo "<h1>Reading content from ITM website!</h1>";
   $ch = curl_init("http://domain.edu/index.php?option=com_content&view=article&id=58&Itemid=375&alias=lms");
   $fp = fopen("example_homepage.txt", "w");

   curl_setopt($ch, CURLOPT_FILE, $fp);
   curl_setopt($ch, CURLOPT_HEADER, 0);

   curl_exec($ch);
   curl_close($ch);
   $my_file="example_homepage.txt";
   $handle = fopen($my_file, 'rb');
   $data = fread($handle,filesize($my_file));

   $contents = strstr(file_get_contents('example_homepage.txt'), 'More quick links');
   $new_content = str_replace('<a href="', '<a href="http://www.domain.edu', $contents);
   $regex = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.\,]*(\?\S+)?)?)*)@';
   $text = preg_replace($regex, '<a href="$1">$1</a>', $new_content);
   //echo $new_content;
   echo $text;
   fclose($fp);
   ?>

电流输出:

http://www.domain.edu/academiccalendar/Notice for final practical.pdf" target="_blank">Title

在此“最终实用.pdf 的通知”中,不显示为 URL,而仅显示为文本。

4

2 回答 2

2

真的,您不应该使用正则表达式进行屏幕抓取。它很慢,最终会破裂。相反,使用 DOM 解析器或简单的DOMDocument

<?php 
//curl bit
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://itmindia.edu/index.php?option=com_content&view=article&id=58&Itemid=375&alias=lms");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$site = curl_exec($curl);
curl_close($curl);



$dom = new DOMDocument();
@$dom->loadHTML($site);

$ret=array();
foreach($dom->getElementsByTagName('a') as $links) {
    //Is pdf
    if(substr($links->getAttribute('href'),-3) == 'pdf'){
        //Assign
        $url   = $links->getAttribute('href');
        $title = trim($links->nodeValue);
        $ret[]=array('url'=>'http://itmindia.edu'.$url,
                     'title'=>(empty($title)?basename($url):$title));
    }
}

print_r($ret);
/* Result
Array
(
    [0] => Array
        (
            [url] => http://itmindia.edu/images/ITM/pdf/ITMU bro june.pdf
            [title] => ITMU Brochure
        )

    [1] => Array
        (
            [url] => http://itmindia.edu/images/ITM/pdf/Report_2012_LR.pdf
            [title] => Annual Report to UGC July 2012
        )

    [2] => Array
        (
            [url] => http://itmindia.edu/admission2012/PhDwinter/Ph. D. application form 2012-13 for dec 2012 admission.pdf
            [title] => Application Form
        )

    [3] => Array
        (
            [url] => http://itmindia.edu/admission2012/PhDwinter/UF_Application_Form.pdf
            [title] => University Fellowship Form
        )
        ...
        ...
*/

//Then to output
foreach($ret as $v){
    echo '<a href="'.$v['url'].'" target="_blank">'.$v['title'].'</a>';
}
?>
于 2012-11-03T15:18:49.010 回答
1

所有你需要的是

echo "<h1>Reading content from ITM website!</h1>";
$ch = curl_init("http://itmindia.edu/index.php?option=com_content&view=article&id=58&Itemid=375&alias=lms");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($result);
foreach ( $dom->getElementsByTagName('a') as $links ) {
    if (pathinfo($links->getAttribute('href'), PATHINFO_EXTENSION) == "pdf") {
        printf("<a href=\"http://itmindia.edu/%s\">%s</a><br />", $links->getAttribute('href'), $links->nodeValue);
    }
}
于 2012-11-03T15:37:36.053 回答