做乒乓球比赛的直播,截取 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);
	}
}
tech/正则表达式.txt · 最后更改: 2010/02/05 02:35 由 newacct
到顶部
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0 红麦软件 红麦软件