求一段正则表达式

$str=<<<html
<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" alt=" 求一段正则表达式,该怎么处理 " ></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);
<br /><font color='#e78608'>------解决方案--------------------</font><br />
<dl class='code'>PHP code<pre class="brush:php;toolbar:false;">
$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" alt=" 求一段正则表达式,该怎么处理 " ></a>
<p><a href="http://www.taobao.com/" class="site">[淘宝商城1]</a></p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2336">
<img src="https://img.php.cn/upload/ai_manual/001/246/273/175937519917802.png" alt="一键职达">
</a>
<div class="aritcle_card_info">
<a href="/ai/2336">一键职达</a>
<p>AI全自动批量代投简历软件,自动浏览招聘网站从海量职位中用AI匹配职位并完成投递的全自动操作,真正实现'一键职达'的便捷体验。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="一键职达">
<span>79</span>
</div>
</div>
<a href="/ai/2336" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="一键职达">
</a>
</div>
<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" alt=" 求一段正则表达式,该怎么处理 " ></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 class="brush:php;toolbar:false;">';
print_r($res);
echo '
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号