首页 > 代码库 > preg_match_all使用实例

preg_match_all使用实例

<?php
$str = <<<EOT
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>微信公众平台</title><link href="https://res.wx.qq.com/mpres/htmledition/images/favicon1e5b3a.ico" rel="Shortcut Icon"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/base/layout_head1f56cd.css"/><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/base/base1f8f18.css"/><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/base/lib1ec5f7.css"/><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/page/setting/setting_common1ec5f7.css"/><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/page/setting/setting_index1f46ee.css"/><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/jquery.Jcrop.min1e5b3a.css"/><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/c/=/mpres/htmledition/style/widget/upload1f8f05.css,/mpres/htmledition/style/widget/processor_bar1ec663.css" /></head><body class="zh_CN"><div class="main_bd"><div class="account_setting_area" id="settingArea"><ul><li class="account_setting_item"><h4>名称</h4><div class="meta_opr"></div><div class="meta_content">青鸢</div></li><li class="account_setting_item"><h4>头像</h4><div class="meta_opr"><a id="changeHeadImg" href="javascript:;">修改头像</a><p class="desc">(一个月内只能申请修改一次)</p></div><div class="meta_content"><img class="avatar" src="/misc/getheadimg?token=1388&fakeid=3084&r=269391" /></div></li><li class="account_setting_item"><h4>登录邮箱</h4><div class="meta_opr"><p><a href="/acct/updateboundemail?t=setting/bind-email&action=bind&token=138856269&step=1&lang=zh_CN">修改</a></p><p class="desc">(一个月内只能申请修改一次)</p></div><div class="meta_content">aaaa@163.com</div></li><li class="account_setting_item"><h4>原始ID</h4><div class="meta_opr"></div><div class="meta_content"><span>gh_00aec11d9</span></div></li><li class="account_setting_item"><h4>微信号</h4><div class="meta_opr"></div><div class="meta_content"><span>aaaa_163</span></div></li><li class="account_setting_item"><h4>类型</h4><div class="meta_opr"></div><div class="meta_content">订阅号</div></li><li class="account_setting_item"><h4>认证情况</h4><div class="meta_opr verify"><a href="javascript:;" id="Js_weiboVerify">关联微博认证</a><a href="/merchant/store?action=detail&t=wxverify/detail&info=verify&lang=zh_CN&token=1388">申请微信认证</a></div><div class="meta_content">未认证</div></li><li class="account_setting_item"><h4>介绍</h4><div class="meta_opr"><p><a id="modifyUserInfo" href="javascript:;" class="">修改</a></p><p class="desc">(一个月内功能介绍只能申请修改一次)</p></div><div class="meta_content">php学习交流</div></li><li class="account_setting_item"><h4>所在地址</h4><div class="meta_opr"><a href="/cgi-bin/settingpage?action=sosomap&lang=zh_CN&token=138856269">设置</a></div><div class="meta_content"></div></li><li class="account_setting_item"><h4>二维码</h4><div class="meta_opr"><a id="more_size" href="javascript:;">更多尺寸</a></div><div class="meta_content"><a target="_blank" class="verifyInfo" title="点击下载" href="/misc/getqrcode?fakeid=3088921&token=1388&style=1&action=download"><img src="/misc/getqrcode?fakeid=3084389217&token=1388&style=1" width="150" /></a></div></li></ul></div></div></div></div><div class="faq"><ul class="links"><li class="links_item no_extra"><a href="http://crm2.qq.com/page/portalpage/wpa.php?uin=40012345&f=1&ty=1&ap=000011:400792:|m:12|f:400792:m:12" target="_blank">在线客服</a></li><li class="links_item"><a href="http://kf.qq.com/product/weixinmp.html" target="_blank">客服中心</a></li></ul><p class="tail">反馈官号weixingongzhong</p></div></div></body></html>
EOT;

preg_match_all("/<li class=\"account_setting_item\">\s*?<h4>(.*?)<\/h4>[\s\S]*?<div class=\"meta_content\">([\s\S]*?)<\/div>/",$str,$out);
//var_dump($out);die;
foreach ( $out [1] as $key=>$val ) {
	$arr [] = array('key'=>$val,'val'=>trim ( $out[2][$key] ));
}
if (count($arr) > 0) {
	foreach ($arr as $k=>$v){
		switch ($v['key']){
			case '原始ID':
				$arr['openid'] = preg_replace("/<span>(.*?)<\/span>/", "$1", $v['val']);
				break;
			case '微信号':
				$arr['account'] = preg_replace("/<span>(.*?)<\/span>/", "$1", $v['val']);
				break;
			case '名称':
				$arr['wx_name'] = $v['val'];
				break;
			case '类型':
				$arr['type'] = ($v['val']=='服务号') ? 1 : 0;
				break;
			case '认证情况':
				$arr['verify'] = strpos($v['val'],"icon_verify_checked")?1:2;
			default:
				$arr[$v['key']] = $v['val'];
		}
	}
	//return $arr;
}	

var_dump($arr);die;	
?>

输出:

array (size=21)
  0 => 
    array (size=2)
      'key' => string '名称' (length=6)
      'val' => string '青鸢' (length=6)
  1 => 
    array (size=2)
      'key' => string '头像' (length=6)
      'val' => string '<img class="avatar" src=http://www.mamicode.com/"/misc/getheadimg?token=1388&fakeid=3084&r=269391" />' (length=77)>

preg_match_all使用实例