Home > Backend Development > PHP Tutorial > php regular expression to match a tag

php regular expression to match a tag

WBOY
Release: 2016-08-04 09:21:01
Original
1622 people have browsed it

<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>
Copy after login
Copy after login

The specific value of the id in the href must match, as well as the value of the a tag, and the content in the following div must also match.

It feels so complicated, please ask God for help.

Reply content:

<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>
Copy after login
Copy after login

The specific value of the id in the href must match, as well as the value of the a tag, and the content in the following div must also match.

It feels so complicated, please ask God for help.

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

<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>
Copy after login

Give me a reference

preg_match_all("/<A.*?id=(.*)'.*>(.*)</A>.*<DIVsclass=STYLE1salign=right>(.*)</DIV&gt ;/Uis", $str,$m);

If you don’t know how to write regular expressions, you can try phpquery. The writing method is similar to jquery, haha

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template