แยก URL ลิงก์ไฟล์ทั้งหมดไปยังรายการจากฟิลด์สตริงฐานข้อมูล mysql

ฉันต้องการรายการ URL ของไฟล์ทั้งหมดในฟิลด์ฐานข้อมูลของฉัน

ฐานข้อมูล mysql, article ตาราง

`id` | `subject` | `content`

ค่า content คือข้อความ html ที่มี URL ไฟล์อย่างน้อย 1 รายการ เช่น:

<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 ประเภท

  1. รูปภาพพร้อมนามสกุล jpg,jpeg,png,bmp,gif
  2. เอกสารที่มีนามสกุล 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);
}

แบ่งปันกับผู้อื่นที่ต้องการตัวอย่างงาน หวังว่าจะช่วยประหยัดเวลาของคุณ


person user3009410    schedule 05.04.2017    source แหล่งที่มา
comment
'ฉันเขียนโค้ดแล้วแต่ใช้งานไม่ได้' ข้อผิดพลาดคืออะไร ปัญหาที่นี่?   -  person hassan    schedule 05.04.2017


คำตอบ (1)


คุณควรเปลี่ยนวงกลางของคุณเป็นดังนี้

$image_list=array(); // prepare an empty array for collection
while ($row=mysql_fetch_array($result))
{
    $text = $row['content'];
    preg_match_all('!http://.+?\.(?:jpe?g|png|gif|pptx?|xlsx?|docx?|pdf|xdw)!i', $s, $matches);
    $img_list=array_merge($image_list,$matches[0]);  // append to array       
}
$writer->writeSheetRow('Sheet1', $image_list);

เนื่องจากคุณไม่ได้ระบุอย่างชัดเจนว่า อะไร ผิด ฉันจึงเดาและดำเนินการต่อ: นิพจน์ทั่วไปแตกต่างจากต้นฉบับของคุณเล็กน้อย และยังรวมถึงวิธีที่ฉันจัดโครงสร้างลูปด้วย (ใช่ เพียงอันเดียว em> เป็นสิ่งจำเป็น) preg_match_all ต้องถูกเรียกเพียงครั้งเดียวสำหรับแต่ละ $text จากนั้นคุณรวมผลลัพธ์จาก $matches[0] ลงในอาร์เรย์ $img_list ของคุณ

ฉันยังลบ U-modifier ของคุณออกด้วย ซึ่งจะเป็นการกลับ "ความโลภ" ของ regexp ทั้งหมด แต่ฉันเพิ่ม ? หลัง + แทนเพื่อทำให้ตัวระบุปริมาณนี้ "ไม่โลภ"

ฉันเตรียมการสาธิตแบบเรียบง่ายไว้ที่นี่: http://rextester.com/JDVMS87065

person Carsten Massmann    schedule 05.04.2017