外部にあるdata.csv内容は以下
001
002
003
このデータを展開して
file_get_contents
でデータを取得しようと思います。
無論csvは3行だけでなく10行近くになることもあります。
取得したいデータは
$str01 = file_get_contents('http://www.hogehoge.com/001/xx.html');
$str02 以下同じくで001が002→003となって行きます
となり各ディレクトリ xx.html を取ってくる形です。
各データの中身は
<html><title>タイトル</title><body>中身</body></html>
タイトルをつまみます
最終的に
タイトル(/001/のもの)
タイトル(/002/のもの)
タイトル(/003/のもの)
となるのが理想です。
ダメソースは以下
mb_regex_encoding('EUC-JP');
if( $handle = fopen( 'data.csv', 'r' ) ){
while( ( $lines = fgetcsv( $handle ) ) !== FALSE ){
for( $i = 0; $i < count( $lines ); $i ++ ){
$str[$i] = file_get_contents('http://www.hogehoge.com/'.$lines[$i]./xx.html'');
preg_match('!<title>*(.*)</title>*!s', $str[$i], $title01[$i])){;
print $title01[$i];
}
}
}
よろしくお願いします。
質問のスクリプトに手を加えてみました。
下記のようにしてください。
なお、各データの中において、最初に <title> タグが現れる場所を取得すると想定しています。
mb_regex_encoding('EUC-JP'); if ($handle = fopen('data.csv', 'r')) { while (!FEOF($HANDLE)) { $lines = fgets($handle, 128); $url = 'http://www.hogehoge.com/'. $lines . '/xx.html'; $str = file_get_contents($url); if (preg_match("/<title>([\<]+)<\/title>/ms", $str, $arr) > 0) { print $arr[1] . "<br />\n"; } } }
質問のスクリプトに手を加えてみました。
下記のようにしてください。
なお、各データの中において、最初に <title> タグが現れる場所を取得すると想定しています。
mb_regex_encoding('EUC-JP'); if ($handle = fopen('data.csv', 'r')) { while (!FEOF($HANDLE)) { $lines = fgets($handle, 128); $url = 'http://www.hogehoge.com/'. $lines . '/xx.html'; $str = file_get_contents($url); if (preg_match("/<title>([\<]+)<\/title>/ms", $str, $arr) > 0) { print $arr[1] . "<br />\n"; } } }
mb_regex_encoding('EUC-JP');
if ($handle = fopen('data.csv', 'r')) {
while (!feof($handle)) {
$lines = fgets($handle, 128);
$url = 'http://www.hogehoge.com/'.$lines.'/xx.html';
$str = file_get_contents($url);
if (preg_match("/<title>
『/xx.html』の前に半角スペースが入ってうまく引っ張ってこれないようです。解決法があればお願い致します。
以下のように修正してください。
$lines = fgets($handle, 128);
↓
$lines = trim(fgets($handle, 128));
ありがとうございます。
これでやってみます。
mb_regex_encoding('EUC-JP');
if ($handle = fopen('data.csv', 'r')) {
while (!feof($handle)) {
$lines = fgets($handle, 128);
$url = 'http://www.hogehoge.com/'.$lines.'/xx.html';
$str = file_get_contents($url);
if (preg_match("/<title>2011/04/15 12:19:04