Heim  >  Artikel  >  Backend-Entwicklung  >  php正则表达式匹配a标签

php正则表达式匹配a标签

WBOY
WBOYOriginal
2016-08-04 09:21:011571Durchsuche

<code><table width="100%" border="0">
    <tbody>
    <tr>
        <td class="line" width="18" align="middle">
            <img  src="../images/yt/d.gif"    style="max-width:90%"  style="max-width:90%" alt="php正则表达式匹配a标签" >
        </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>
    </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"    style="max-width:90%"  style="max-width:90%" alt="php正则表达式匹配a标签" >
        </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>
    </tbody>
</table></code>

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

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

<code>/<a>(.*?)/i</a></code>

<code><?php $string = "<table width='100%' border='0'>
    <tbody>
    <tr>
        <td class="line" width="18" align="middle">
            <img  src="../images/yt/d.gif"    style="max-width:90%"  style="max-width:90%" alt="php正则表达式匹配a标签" >
        </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>
    </tbody>
";

    $reg = "#.*(.*)(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.>(.*).*<div>(.*)/Uis", $str,$m); <p class="answer fmt" data-id="1020000006006753"> </p> <p>如果不会写正则 可以试试 phpquery 写法跟jquery差不多 哈哈</p> </div></a.>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn