首頁  >  文章  >  後端開發  >  php正規表示式符合a標籤

php正規表示式符合a標籤

WBOY
WBOY原創
2016-08-04 09:21:011571瀏覽

<code><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></code>

其中href中的id的具體值要匹配到,還有a標籤的值,還有後面的div中的內容也要匹配。

感覺好複雜,求大神幫忙。

回覆內容:

<code><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></code>

其中href中的id的具體值要匹配到,還有a標籤的值,還有後面的div中的內容也要匹配。

感覺好複雜,求大神幫忙。

<code>/<a(.*?)href="(.*?)id"(.*?)>(.*?)<\/a>/i</code>

<code><?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];</code>

給個參考

preg_match_all("/<a.>(.*).*<divsclass>(.*)/Uis", $str,$m);</divsclass></a.>

如果不會寫正規 可以試試 phpquery 寫法跟jquery差不多 哈哈

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn