码迷,mamicode.com
首页 > 其他好文 > 详细

preg_match_all使用实例

时间:2014-10-14 08:45:08      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   os   使用   ar   java   for   

<?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="/misc/getheadimg?token=1388&fakeid=3084&r=269391" />' (length=77)
  2 => 
    array (size=2)
      'key' => string '登录邮箱' (length=12)
      'val' => string 'aaaa@163.com' (length=12)
  3 => 
    array (size=2)
      'key' => string '原始ID' (length=8)
      'val' => string '<span>gh_00aec11d9</span>' (length=25)
  4 => 
    array (size=2)
      'key' => string '微信号' (length=9)
      'val' => string '<span>aaaa_163</span>' (length=21)
  5 => 
    array (size=2)
      'key' => string '类型' (length=6)
      'val' => string '订阅号' (length=9)
  6 => 
    array (size=2)
      'key' => string '认证情况' (length=12)
      'val' => string '未认证' (length=9)
  7 => 
    array (size=2)
      'key' => string '介绍' (length=6)
      'val' => string 'php学习交流' (length=15)
  8 => 
    array (size=2)
      'key' => string '所在地址' (length=12)
      'val' => string '' (length=0)
  9 => 
    array (size=2)
      'key' => string '二维码' (length=9)
      'val' => string '<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>' (length=214)
  'wx_name' => string '青鸢' (length=6)
  '头像' => string '<img class="avatar" src="/misc/getheadimg?token=1388&fakeid=3084&r=269391" />' (length=77)
  '登录邮箱' => string 'aaaa@163.com' (length=12)
  'openid' => string 'gh_00aec11d9' (length=12)
  'account' => string 'aaaa_163' (length=8)
  'type' => int 0
  'verify' => int 2
  '认证情况' => string '未认证' (length=9)
  '介绍' => string 'php学习交流' (length=15)
  '所在地址' => string '' (length=0)
  '二维码' => string '<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>' (length=214)

preg_match_all使用实例

标签:des   style   http   io   os   使用   ar   java   for   

原文地址:http://blog.csdn.net/moqiang02/article/details/40070745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!