首页 > php正则表达式匹配a标签

php正则表达式匹配a标签

<table width='100%' border='0'>
    <TBODY>
    <tr>
        <TD class=line width=18 align=middle>
            <IMG src='../images/yt/d.gif' width=4 height=7>
        </TD>
        <TD class=line align=left><A class=newslink
                                     href='xxxxx?id=1624'
                                     title='xxxx'
                                     target=_blank>要匹配的内容</A></TD>
        <TD class=line width=75>
            <DIV class=STYLE1 align=right>要匹配的内容</DIV>
        </TD>
    </TR>
    </tr></TBODY>
</table>

其中href中的id的具体值要匹配到,还有a标签的值,还有后面的div中的内容也要匹配。

感觉好复杂,求大神帮忙。


<?php

    $string = "<table width='100%' border='0'>
    <TBODY>
    <tr>
        <TD class=line width=18 align=middle>
            <IMG src='../images/yt/d.gif' width=4 height=7>
        </TD>
        <TD class=line align=left><A class=newslink
                                     href='xxxxx?id=1624'
                                     title='xxxx'
                                     target=_blank>要匹配的内容</A></TD>
        <TD class=line width=75>
            <DIV class=STYLE1 align=right>要匹配的内容</DIV>
        </TD>
    </TR>
    </tr></TBODY>
</table>";

    $reg = "#.*<(a|A).*href=['\"].*\?id=(\d+)['\"].*>(.*)</(a|A)>.*<DIV class=STYLE1 align=right>(.*)</DIV>#isU";

    preg_match($reg, $string, $matchs);

    // a href id
    $id = $matchs[2];

    // a content
    $content = $matchs[3];

    // div content
    $divContent = $matchs[5];

给个参考


如果不会写正则 可以试试 phpquery 写法跟jquery差不多 哈哈


preg_match_all("/<A.*\?id=(.*)'.*>(.*)<\/A>.*<DIV\sclass=STYLE1\salign=right>(.*)<\/DIV>/Uis", $str,$m);


/<a(.*?)href="(.*?)id"(.*?)>(.*?)<\/a>/i
【热门文章】
【热门文章】