<ul class="s">
<li id="tongji_cate_1_pn_1_rank_1">
<a href="http://www.tmall.com" class="pic">
<img src="http://image.com/pic.gif"></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城]</a></p>
<p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p>
</li>
<li id="tongji_cate_2_pn_2_rank_2">
……
</li>……<ul>
我希望通过正则 将上面的内容 变成一串数组array(
[0] =>array(
"shopname" => "淘宝商城",
"activename"=> "活动标题",
"actimage" => "http://image.com/pic.gif",
"acturl" => "http:/www.tmall.com/" //这个是活动标题的链接
),
[1] =>array(……)
……
);就像上面一样输出数组
<li id="tongji_cate_1_pn_1_rank_1">
<a href="http://www.tmall.com" class="pic">
<img src="http://image.com/pic.gif"></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城]</a></p>
<p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p>
</li>
<li id="tongji_cate_2_pn_2_rank_2">
……
</li>……<ul>
我希望通过正则 将上面的内容 变成一串数组array(
[0] =>array(
"shopname" => "淘宝商城",
"activename"=> "活动标题",
"actimage" => "http://image.com/pic.gif",
"acturl" => "http:/www.tmall.com/" //这个是活动标题的链接
),
[1] =>array(……)
……
);就像上面一样输出数组
解决方案 »
- 下面的这段PHP代码应该怎么改红字部分的变量才有值
- php高并发应该用Apache还是IIS
- w3cshool chm 包含N多处WEB的电子手册
- PHP写入数据库的问题
- MYSQL问题
- Cannot modify header information 这是什么意思,如何解决?
- sockets问题
- 简单问题(高分,在线等侍):关于大数字转换字符串问题!!!
- 怎样让php页面自动跳转?
- 萌新瑟瑟发抖,关于超链接
- 网站内网测试良好,外网速度超级慢!框架用的CI,连接SQL2000数据库。
- 谁能看出这条语句有什么错误:$sql="select * from huiyuanka where userName=$sousuoname"
<ul class="s">
<li id="tongji_cate_1_pn_1_rank_1">
<a href="http://www.tmall.com" class="pic">
<img src="http://image.com/pic.gif"></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城]</a></p>
<p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p>
</li>
<li id="tongji_cate_2_pn_2_rank_2">
……
</li>……<ul>
html;
preg_match_all('#<li[^>]*>(.*)</li>#isU',$str,$arr);
foreach($arr[1] as $v){
preg_match_all('#<a\s*href="(.*)"[^>]*>(.*)</a>#isU',$v,$ar[]);
}
print_r($ar);
$str = <<<HTML
<ul>
<li id="tongji_cate_1_pn_1_rank_1">
<a href="http://www.tmall.com" class="pic">
<img src="http://image.com/pic.gif1"></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城1]</a></p>
<p><a href="http://www.tmall.com/1" title="活动标题1" >活动标题</a></p>
</li>
<li id="tongji_cate_2_pn_2_rank_2">
<a href="http://www.tmall.com" class="pic">
<img src="http://image.com/pic.gif2"></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城2]</a></p>
<p><a href="http://www.tmall.com/2" title="活动标题2" >活动标题</a></p>
</li>
</ul>
HTML;
/**
*"shopname" => "淘宝商城",
"activename"=> "活动标题",
"actimage" => "http://image.com/pic.gif",
"acturl" => "http:/www.tmall.com/" //这个是活动标题的链接
*/$pattern = '~img.*(?<=")(.*)".*\[(.*)\].*(?<=f=")(.*)".*(?<=e=")(.*)"~Uis';
preg_match_all($pattern, $str, $m);$res = array();
$num = 4;//匹配项数量
for ($i = 0; $i < count($m[0]); $i++){
for ($j = 1; $j <= $num; $j++){
$res[$i][] = $m[$j][$i];
}
}
echo '<pre>';
print_r($res);
echo '</pre>';
/**
剩下的你懂的
Array
(
[0] => Array
(
[0] => http://image.com/pic.gif1
[1] => 淘宝商城1
[2] => http://www.tmall.com/1
[3] => 活动标题1
) [1] => Array
(
[0] => http://image.com/pic.gif2
[1] => 淘宝商城2
[2] => http://www.tmall.com/2
[3] => 活动标题2
))
*/
<?php$infos = array();$keynames = array('acturl','actimage','shopname','activename');$str = '<ul class="s">
<li id="tongji_cate_1_pn_1_rank_1">
<a href="http://www.tmall.com" class="pic">
<img src="http://image.com/pic.gif"></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城]</a></p>
<p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p>
</li>
<li id="tongji_cate_2_pn_2_rank_2">
<a href="http://www.tmall22.com" class="pic">
<img src="http://image.com/pic2.gif"></a>
<p><a href="http://www.360buy.com/" class="site">[京东商城]</a></p>
<p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p>
</li>……<ul>';preg_match_all('/<a\s+href="([^"]+?)"\s+class="pic">.*?<img\s+src="([^"]+?)">.*?<a\s+href="[^"]+"\s+class="site">\[([^\]]+?)\]<\/a>.*?<a\s+href="[^"]+"\s+title="活动标题"\s*>([^<]+)<\/a>/is',$str,$matchs);array_shift($matchs);$row = count($matchs[0]);
$col = count($matchs);for($j=0;$j<$row;$j++){
for($i=0;$i<$col;$i++){
$infos[$j][$keynames[$i]] = $matchs[$i][$j];
}
}
var_dump($infos);output:
array
0 =>
array
'acturl' => string 'http://www.tmall.com' (length=20)
'actimage' => string 'http://image.com/pic.gif' (length=24)
'shopname' => string '淘宝商城' (length=12)
'activename' => string '活动标题' (length=12)
1 =>
array
'acturl' => string 'http://www.tmall22.com' (length=22)
'actimage' => string 'http://image.com/pic2.gif' (length=25)
'shopname' => string '京东商城' (length=12)
'activename' => string '活动标题' (length=12)