【问题标题】:Regex for recognizing space in file name用于识别文件名中的空格的正则表达式
【发布时间】:2023-03-26 12:35:01
【问题描述】:

我想使用正则表达式来识别 .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,而仅显示为文本。

【问题讨论】:

  • 对不起,这不是我自己的文件。我正在从另一个网站获取这些指向 pdf 文件的链接。
  • 让我告诉你你想从其他网站获取 PGF 链接的字符串 ????如果是这样,您如何加载文件内容???
  • 嗯,我想我自己解决了:/ $regex = '@((https?://)?([-\w ]+\.[-\w\.]+)+\w(:\d+)?(/([-\w /_\.\,]*(\?\S+)?)?)*)@';
  • 对我来说仍然看起来有点过头了......这就是为什么我问你是如何加载内容的......它们是更简单的方法
  • P可能还是太简单了 (https?:\/\/)?([-\w]+\.[-\w\.]+)+\w(:\d+)? (\/.*\.pdf)

标签: php


【解决方案1】:

确实,您不应该使用正则表达式进行屏幕抓取。它很慢,最终会破裂。相反,使用 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>';
}
?>

【讨论】:

  • 谢谢,但我刚刚更新了我的代码。能否请您再看一遍并告诉我我可以对其进行哪些更改?
  • 谢谢,这正是我想要的! :)
  • 还有一件事。有没有办法识别字符串“更多快速链接”并使用您的方法仅在此字符串之后打印 URL?就像在我的代码中一样,我使用$contents = strstr(file_get_contents('example_homepage.txt'), 'More quick links'); 来检查该字符串。无论在文本文件中的何处找到该特定字符串,它都会开始打印文件的其余内容。
【解决方案2】:

你只需要

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多