別のページから一部分を引っ張ってくるphp関数

手っ取り早く、ページ内に他のページのヘッダー部分とかメニューの一部とかを表示したい、と思うことが結構あるので、再利用できるように関数化しました。

function ripping_html($source,$start,$end){
$html_source = file_get_contents("$source");
$rip_source = preg_replace('/\n/', ' ', $html_source);
$pattern= "/$start(.*)$end/";
preg_match($pattern, $rip_source, $match);
echo $match[0];
}

広告

使い方

引っ張ってくる対象ページのURLと、HTMLソース内の開始シグナル、終了シグナルを引数で指定します。

string ripping_html  ( resource $context, string $start , string $end )

ripping_html('http://blog.dacelo.info/example.html', '<html>', '<end>');

About: dacelo


1 thought on “別のページから一部分を引っ張ってくるphp関数”

Leave a Reply

Your email address will not be published. Required fields are marked *