做乒乓球比赛的直播,截取 http://www.ittf.com/competitions/_groups_matches.asp?grp=&Event_Type=msp&Competition_ID=1625 这种无规则的恶心的数据,练就了正则表达式的各种技巧。
取页面编码:
$c = '<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META http-equiv=Content-Type content="text/html; charset=gb2312">
';
preg_match_all('|<meta[^>]+>|is', $c, $out);
foreach ($out[0] as $val) {
if (stripos($val, 'content-type') !== false) {
preg_match("|charset=([a-z0-9\-]+)\b|i", $val, $code);
print_r($code);
}
}
过滤display:none的内容
if(preg_match('|<([a-zA-Z]*?)\s+(.*?)style(\s+)?=(\s+)?[\'"]([^>"]*?)display(\s+)?:(\s+)?none;([^>"]*?)[\'"]([^>]*?)>|im',$str,$match)){ $meta_tag = $match[1]; if(preg_match('|<'.$meta_tag.'\s+(.*?)style(\s+)?=(\s+)?[\'"]([^>"]*?)display(\s+)?:(\s+)?none;([^>"]*?)[\'"]([^>]*?)>(.*?)</'.$meta_tag.'>|ims',$str,$block)){ $str = str_replace($block[0],'',$str); } }到顶部