我需要在我的數據庫字段之一中獲取所有文件url的列表。從mysql數據庫字符串字段中提取所有文件鏈接url到列表
mysql數據庫,article
表
`id` | `subject` | `content`
的content
值是HTML文本與一個或多個文件的URL,例如:
<p>this is the answer for ..., you can refer to below screenshot:</p>
<img src="http://the_url_of_image_here/imagename.jpg/>
<p>or refer to below document</p>
<a href="http://the_url_of_doc_here/guide.ppt>guide</a>
<a href="http://the_url_of_doc_here/sample.dox>sample</a>
有2種類型的文件
- 圖片,擴展名爲jpg,jpeg,png,bmp,gif
- 文件,擴展名爲DOC,DOCX,PPT,PPTX,XLS,XLSX,PDF,XPS
我做了很多GOOLGE,看起來很難只用MySQL來做到這一點,PHP將使它容易,我寫我的代碼,但它不能工作。
謝謝cars10,我解決了它。
function export_articles_link()
{
global $date_from, $date_to;
$filename = "kb_articles_link_".$date_from."_".$date_to.".xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$query = 'SELECT `content` FROM `kb_articles` WHERE ((DATE(`dt`) BETWEEN \'' . $date_from . '\' AND \'' . $date_to . '\') AND (`content` LIKE \'%<img src=%\' or `content` LIKE \'%<a href="http:%\')) order by id asc';
$result = mysql_query($query);
$writer = new XLSXWriter();
$img_list = array();
while ($row=mysql_fetch_array($result))
{
$text = $row['content'];
preg_match_all('!http://.+\.(?:jpe?g|png|gif|ppt?|xls?|doc?|pdf|xdw)!Ui', $text, $matches);
$img_list = $matches[0];
foreach ($img_list as $url)
{
$writer->writeSheetRow('Sheet1', array($url)); // if more than one url it will be put on first column
}
};
$writer->writeToStdOut();
exit(0);
}
與其他需要工作示例的人分享,希望能節省您的時間。
「我寫我的代碼,但它不能正常工作」有什麼錯誤?這裏的問題? – hassan