Home >Backend Development >PHP Tutorial >php regular expression to match a tag
<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>
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><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>
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>
<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>
Give me a reference
preg_match_all("/<A.*?id=(.*)'.*>(.*)</A>.*<DIVsclass=STYLE1salign=right>(.*)</DIV> ;/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