【发布时间】: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