来自html的file_get_contents爆炸,写入电子表格的单元格

时间:2016-08-28 14:44:58

标签: php csv spreadsheet file-get-contents fputcsv

我尝试实现的是通过file_get_contents()从URL的源代码中剔除特定内容,然后爆炸()在内容所在的位置附近标记,仅返回HTML格式内容,然后将其写入电子表格或CSV的单个单元格。很简单,我想。

这就是我所拥有的:

<?php

//My .html

$url = 'http://spiderlearning.com/demo/ALG_SA_U1_L1.html';

//Get content

$content = file_get_contents($url);

//Get content sections

$lesson_name = explode( '<section id="nameField" class="editable" contenteditable="false">' , $content);

$section_title1 = explode( '<a onclick="goToByScroll(\'obj0\')" href="#">' , $content);

$challenge_q = explode( '<section id="redactor_content" class="editable" contenteditable="false">' , $content);

//Write content

$write1 = explode("</section>" , $lesson_name[1]);
$write2 = explode("</a>" , $section_title1[1]);
$write3 = explode("</section>" , $challenge_q[1]);

//Into arrays

$line1 = array($write1[0],$write2[0],$write3[0]);

$list = array($line1);

//Open .csv

$file = fopen("data/data.csv", "w");

//Write as line, delimitate with ";"

foreach ($list as $line) fputcsv($file, $line, ';');

//Close

fclose($file);

?>

返回:

CSV

Excel

我正在寻找的是:

CSV:

Unit 1 Lesson 1; 1. Challenge Questions; <p><img src="https://s3-eu-west-1.amazonaws.com/teacher-uploads.fishtree.com/SpiderLearning/1428953716a42b06b9-1ce1-4594-badd-4ab8c9b65ac0.jpeg" alt="" rel="float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;" style="float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;"></p><p>Before you begin this lesson, let's see what you already know about the topic. Take a moment to complete the three Challenge Questions that follow.</p>

在我看来,问题是格式化内容中的回车。它也会在返回的内容周围拾取括号,但我不确定从哪里开始。有没有办法逃避这些?我过去总是把类似的功能放在一起,没有任何问题,但这是我的第一个file_get_contents()到CSV,而且我在几周后终于遇到了它。

2 个答案:

答案 0 :(得分:0)

首先摆脱换行符这样做: foreach ($list as $line) fputcsv($file, preg_replace( "/\r|\n/", "", $line), ';');

最好留下fputcsv引入的字段分隔符。原因是其中一个字段中的任何分号都会将您的CSV打破到您想要的CSV之上:

"Unit 1 Lesson 1";"1. Challenge Questions";"<p><img src=""https://s3-eu-west-1.amazonaws.com/teacher-uploads.fishtree.com/SpiderLearning/1428953716a42b06b9-1ce1-4594-badd-4ab8c9b65ac0.jpeg"" alt="""" rel=""float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;"" style=""float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;""></p><p>Before you begin this lesson, let's see what you already know about the topic. Take a moment to complete the three Challenge Questions that follow.</p>"

但是在大​​多数情况下你无法在excel中直接打开它(某处有全局设置)。您需要导入此数据,然后设置以下内容:

settings for import

答案 1 :(得分:0)

这是基于PHP的DOMDocument类的替代解决方案:

$url = 'http://spiderlearning.com/demo/ALG_SA_U1_L1.html';
// Load HTML via DOMDocument class
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile($url);
// Extract the elements of interest
$xpath = new DOMXPath($doc);
$list = [
    [
        "lesson" => $doc->getElementById('nameField')->textContent,
        "section" => $xpath->query("//div[@class='activitySelect']//a")[0]->textContent,
        "challenge" => innerHTML($doc->getElementById('redactor_content'))
    ]
];
// Write CSV (unchanged code)
$file = fopen("php://output", "w");
foreach ($list as $line) fputcsv($file, $line, ';');
fclose($file);

// Utility function
function innerHTML($node) {
    return implode(array_map([$node->ownerDocument,"saveHTML"], 
                             iterator_to_array($node->childNodes)));
}