首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

帮忙写个正则表达式,该怎么解决

2013-07-16 
帮忙写个正则表达式dd classclearatarget_blank title爱丽丝cos href/cp/sK7A9su_Y29z.html

帮忙写个正则表达式

<dd class="clear">
   <a  target="_blank" title="爱丽丝cos" href="/cp/sK7A9su_Y29z.html">爱丽丝cos</a>
<a  target="_blank" title="爱丽丝连衣裙" href="/cp/sK7A9su_wazSwsi5.html">爱丽丝连衣裙</a>
<a  target="_blank" title="爱丽丝梦游仙境" href="/cp/sK7A9su_w87Tzs_JvrM.html">爱丽丝梦游仙境</a>
<a  target="_blank" title="爱丽丝疯狂回归" href="/cp/sK7A9su_t-i_8bvYuek.html">爱丽丝疯狂回归</a>
<a  target="_blank" title="爱丽丝花盆" href="/cp/sK7A9su_u6jF6A.html">爱丽丝花盆</a>
<a  target="_blank" title="爱丽丝手办" href="/cp/sK7A9su_ytaw7A.html">爱丽丝手办</a>
<a  target="_blank" title="alice" href="/cp/YWxpY2U.html">alice</a>
<a  target="_blank" title="女仆" href="/cp/xa7GzQ.html">女仆</a>
<a  target="_blank" title="爱丽丝学园" href="/cp/sK7A9su_0afUsA.html">爱丽丝学园</a>
<a  target="_blank" title="爱丽丝菲尔" href="/cp/sK7A9su_t8a2-w.html">爱丽丝菲尔</a>
<a  target="_blank" title="爱丽丝手包" href="/cp/sK7A9su_ytaw_A.html">爱丽丝手包</a>
<a  target="_blank" title="爱丽丝复古" href="/cp/sK7A9su_uLS5xQ.html">爱丽丝复古</a>
</dd>

我现在要取这个页面的<dd class="clear">到</dd>之间这段内容里的title的值和href的值,因为这个页面其他地方也有这种格式,所以现在直接用
$contents_key = '~<a  target="_blank" title="(.*?)" href="(.*?)">~';
preg_match_all($contents_key,$html,$contentskey);
这样会把dd标签外的相同格式的也包含进来,问下这个正则应该怎么写?
[解决办法]
$s=<<<div
agfdafg
<dd class="clear">
                                                   <a  target="_blank" title="爱丽丝cos" href="/cp/sK7A9su_Y29z.html">爱丽丝cos</a>
                            <a  target="_blank" title="爱丽丝连衣裙" href="/cp/sK7A9su_wazSwsi5.html">爱丽丝连衣裙</a>


                            <a  target="_blank" title="爱丽丝梦游仙境" href="/cp/sK7A9su_w87Tzs_JvrM.html">爱丽丝梦游仙境</a>
                            <a  target="_blank" title="爱丽丝疯狂回归" href="/cp/sK7A9su_t-i_8bvYuek.html">爱丽丝疯狂回归</a>
                            <a  target="_blank" title="爱丽丝花盆" href="/cp/sK7A9su_u6jF6A.html">爱丽丝花盆</a>
                            <a  target="_blank" title="爱丽丝手办" href="/cp/sK7A9su_ytaw7A.html">爱丽丝手办</a>
                            <a  target="_blank" title="alice" href="/cp/YWxpY2U.html">alice</a>
                            <a  target="_blank" title="女仆" href="/cp/xa7GzQ.html">女仆</a>
                            <a  target="_blank" title="爱丽丝学园" href="/cp/sK7A9su_0afUsA.html">爱丽丝学园</a>
                            <a  target="_blank" title="爱丽丝菲尔" href="/cp/sK7A9su_t8a2-w.html">爱丽丝菲尔</a>
                            <a  target="_blank" title="爱丽丝手包" href="/cp/sK7A9su_ytaw_A.html">爱丽丝手包</a>
                            <a  target="_blank" title="爱丽丝复古" href="/cp/sK7A9su_uLS5xQ.html">爱丽丝复古</a>
                        </dd>
dafdsaf
div;
preg_match('/<dd class="clear">(.+?)<\/dd>/s',$s,$m);


echo $m[1];



                                                   <a  target="_blank" title="爱丽丝cos" href="/cp/sK7A9su_Y29z.html">爱丽丝cos</a>
                            <a  target="_blank" title="爱丽丝连衣裙" href="/cp/sK7A9su_wazSwsi5.html">爱丽丝连衣裙</a>
                            <a  target="_blank" title="爱丽丝梦游仙境" href="/cp/sK7A9su_w87Tzs_JvrM.html">爱丽丝梦游仙境</a>
                            <a  target="_blank" title="爱丽丝疯狂回归" href="/cp/sK7A9su_t-i_8bvYuek.html">爱丽丝疯狂回归</a>
                            <a  target="_blank" title="爱丽丝花盆" href="/cp/sK7A9su_u6jF6A.html">爱丽丝花盆</a>
                            <a  target="_blank" title="爱丽丝手办" href="/cp/sK7A9su_ytaw7A.html">爱丽丝手办</a>
                            <a  target="_blank" title="alice" href="/cp/YWxpY2U.html">alice</a>
                            <a  target="_blank" title="女仆" href="/cp/xa7GzQ.html">女仆</a>
                            <a  target="_blank" title="爱丽丝学园" href="/cp/sK7A9su_0afUsA.html">爱丽丝学园</a>
                            <a  target="_blank" title="爱丽丝菲尔" href="/cp/sK7A9su_t8a2-w.html">爱丽丝菲尔</a>
                            <a  target="_blank" title="爱丽丝手包" href="/cp/sK7A9su_ytaw_A.html">爱丽丝手包</a>


                            <a  target="_blank" title="爱丽丝复古" href="/cp/sK7A9su_uLS5xQ.html">爱丽丝复古</a>
                        
[解决办法]
试试用这个匹配dd的内容


$pattern = '/<dd class="clear">.*<\/dd>/is'

修饰符i:不分大小写
修饰符s:模式里的一个"点"将匹配所有字符,包括换行.没有他,换行将被排除在外.这个修饰符等同于Perl里面的/s修饰符

热点排行