2008年6月19日木曜日

p.130 PHP

utano

p.130のPHPコードです。
ウェブのテキストだけ抽出する(HTMLタグどける)ものです。
こんなかんじ。

/* AIRNow Web Page Scraper
Language: PHP
*/

$readParticles = 0; //flag telling you the next time is the particle value
$particles = -1;

//Define variables
//url of the page with the air quality index data for New York City
$url = 'http://airnow.gov/index.cfm?action=airnow.showlocal&CityID=164';

//open the file at the url for reading;
$filePath = fopen($url, "r");

//as long as you haven't reached the end of the file:
while(!feof($filePath)){
//read one line at a time, and strip all HTML and PHP tags from the line
$line = fgetss($filePath, 4096);
echo $line;
}
//close the file at the URL , you're done
fclose($filePath);
?>


p.131と合体させたもの。
これはテキスト化したウェブから、
ある文字列が出たあとの後ろのテキストを抽出するコード。
こんなかんじ。

/* AIRNow Web Page Scraper
Language: PHP
*/

$readParticles = 0; //flag telling you the next time is the particle value
$particles = -1;

//Define variables
//url of the page with the air quality index data for New York City
$url = 'http://airnow.gov/index.cfm?action=airnow.showlocal&CityID=164';

//open the file at the url for reading;
$filePath = fopen($url, "r");

//as long as you haven't reached the end of the file:
while(!feof($filePath)){
//read one line at a time, and strip all HTML and PHP tags from the line
$line = fgetss($filePath, 4096);

if($readParticles == 1){
$particles = trim($line);
echo "";
$readParticles = 0;
}

if(preg_match('/AQI observed at /', $line)){
if($particles == -1){
$readParticles = 1;
}
}

}
//close the file at the URL , you're done
fclose($filePath);
?>

0 件のコメント: