[#syntax-php,{};

set_time_limit(0);

ini_set('memory_limit','1024M');

$data = file_get_contents(__DIR__.'/data.html');

$title = "";

$key = "";

$desc = "";

$title_pattern = '/<\S*title\s*>(.*?)<\s*\/\s*title\s*>/i';

$key_pattern_l = '/]*)[\'"]\s+[\/>]+/i';

$key_pattern_r = '/]*)[\'"]\s+name[=\s]+[\'"]keywords[\'"]\s+[\/>]+/i';

$desc_pattern_l = '/]*)[\'"]\s+[\/>]+/i';

$desc_pattern_r = '/]*)[\'"]\s+name[=\s]+[\'"]description[\'"\s]+[\/>]+/i';

preg_match($title_pattern,$data,$tmp);

if(isset($tmp[1])){

$title = $tmp[1];

}

preg_match($key_pattern_l,$data,$tmp);

if(isset($tmp[1])){

$key = $tmp[1];

}else{

preg_match($key_pattern_r,$data,$tmp);

if(isset($tmp[1])){

$key = $tmp[1];

}

}

preg_match($desc_pattern_l,$data,$tmp);

if(isset($tmp[1])){

$desc = $tmp[1];

}else{

preg_match($desc_pattern_r,$data,$tmp);

if(isset($tmp[1])){

$desc = $tmp[1];

}

}

var_dump($title,$key,$desc);

#]

首页 | php | 正则 | 获取html页面meta信息 | java | nodejs | html