hoge.ne.jp から IDと価格を抜き出し、それをCSVファイルに書き込むPHPを作りました。
<?php header("Content-Type: text/html; charset=UTF-8"); echo getPageTitle("http://hoge.ne.jp"); function getPageTitle( $url ) { $html = file_get_contents($url); $html = mb_convert_encoding($html, mb_internal_encoding(), "auto" ); if (preg_match( '/<td class="id">(.*?)<\/td>/is', $html, $matches)) { if (preg_match( '/<td class="price">(.*?)<\/td>/is', $html, $matches2)) { $id = $matches[1]; $price = $matches2[1]; $pc = array($id,$price); var_dump($pc); $fp = fopen("test.csv", 'w'); foreach ($pc as $fields) { fputcsv($fp, $fields); } fclose($fp); } } else { return false; } } ?>上記の結果は下記になります。
array (size=2)
0 =>
array (size=4)
0 => string 'apple' (length=5)
1 => string 'orange' (length=6)
2 => string 'banana' (length=6)
3 => string 'pear' (length=4)
1 =>
array (size=4)
0 => string '140' (length=3)
1 => string '100' (length=3)
2 => string '90' (length=2)
3 => string '200' (length=3)
CSVファイルには
apple,orange,banana,pear
140,100,90,200
と記録されます。
【やりたいこと】
CSVファイルに
apple,140
orange,100
banana,90
pear,200
と記録させたいです。
なにか良い方法はありませんでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/30 21:16