2016-08-28 4 views
1

のセルへの書き込み、爆発(のfile_get_contents経由でURLのソースから特定のコンテンツのピンセットである)、そして(爆発)そのコンテンツが命の周りのマークアップ、帰国ちょうどHTMLコンテンツをフォーマットし、その後、スプレッドシートまたはCSVの単一セルに書き込みます。簡単だと思った。返しのfile_get_contentsは私が達成しようとしていますどのようなスプレッドシート

<?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に変換し、Iでの数週間は、最終的にそれを壁にヒットしました。 fputcsvによって導入されたもの、フィールドの区切り文字を残す方が良いでしょう foreach ($list as $line) fputcsv($file, preg_replace("/\r|\n/", "", $line), ';');

+0

あなたのアプローチが理解できる:ここでは

settings for import

pregmatch

+0

謝罪、私のマシンに保存されています。あなたが私が働いているものを見ることができるように、私はこれを主催しました。すべてのファイルはまったく同じ方法で設定され、変更や更新はありません。私はちょうどこの特定の情報がきれいな方法でスプレッドシートになっているので、私が作ったCSVからAPIを使ってCMSにコンテンツを見つけたり置き換えたりコピーしたりしてすばやく更新することができますセンス。 – SJGaliardi

答えて

0

まず改行を取り除くためには、これを行います。その理由は、フィールドの1内部の任意のセミコロンは、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でこれを開くことができません(グローバル設定がどこかにあります) 。あなたは次のことを、このデータをインポートして設定する必要があります。このため

$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))); 
} 
+0

ノーマン、これは完璧に動作しました、ありがとう!単純な問題に対する簡単な解決策。 – SJGaliardi

0

はPHPののDOMDocumentクラスに基づいて、代替ソリューションです。 htmlのWebページが変更されたらどうなりますか?その後、あなたの "爆発"効果はもう働きません。あなたは、たとえばhttps://github.com/paquettg/php-html-parserのために、クラス名、html要素のタイプと同様の経由してWebページからデータを抽出するためのいくつかのライブラリを使用する必要があります。私はローカルファイル(約4,000それらの)に対してこれを実行していることになるコンテキストの
関連する問題