0

td 内の Web ページからデータをフィルタリングしようとしています。これは次のようなものです。

    <td colspan="2">several anchor,bold and other html tags are inside this td</td>

私はこの preg_match を使用しましたが、他のすべての td の出力を提供していますが、上記の場合は出力を提供していません。

    preg_match("/\<td colspan\=\"2\"\>(.*)\<\/td\>/",$str,$title);

完全な td は次のとおりです。

    <td colspan="2">
      <div align="left" style="width:370; height:315;">
            <ins style="display:inline-table;border:none;height:280px;margin:0;padding:0;position:relative;visibility:visible;width:336px">      

          <ins style="display:block;border:none;height:280px;margin:0;padding:0;position:relative;visibility:visible;width:336px" id="aswift_1_anchor"><iframe width="336" scrolling="no" height="280" frameborder="0" style="left:0;position:absolute;top:0;" name="aswift_1" id="aswift_1" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){w.location.replace(h)}}" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0"></iframe></ins></ins>
           </div><p>  When starting out sometimes it is a good idea to write down your            <a href="#" style="text-decoration: underline !important;position:static;font-family:inherit !important;font-weight:inherit !important;font-size:inherit !important;" class="kLink" id="KonaLink1">   
      <font color="blue" style="color: blue !important; font-family:inherit !important;font-weight:inherit !important;font-size:inherit !important;position:static;">                   <span style="color: blue !impor  If you seriously want to take back control of your money you need to build a <a href="http://ezinearticles.com/?To-Set-Up-a-Personal-Budget-Get-a-Pencil-and-Paper&amp;id=1629478">Personal Budget</a>. To learn more about creating a budget please visit the website <a href="http://household-budget.home-choices-net.com">Household Budgets by clicking here</a>. </p><p> </p><p><!-- google_ad_section_end -->

              </p><p>
        <font style="color:02679D; font-size:12"><b><font color="000000">Related Articles - 

       </font>
           </b></font>
        </p><p><table width="100%" border="0"><tbody><tr>
        <td align="center">
           <br><br><br><br>

        <br><br>

          </font></p></td></tr></tbody></table>
            </p></td>
4

2 に答える 2

1

通常、html の解析には正規表現を使用しないでください。ただし、問題は、正規表現が準備ができており、可能なすべてのデータをキャッチすることです。疑問符を追加してみてください:

preg_match("/\<td colspan\=\"2\"\>(.*?)\<\/td\>/",$str,$title);

クエスチョン マークはグループを非グレッディーにし、文字列は次の可能性のあるマークで終了します。

于 2013-03-24T10:56:15.340 に答える
0

修飾子を追加する必要があります:

        preg_match("/\<td colspan\=\"2\"\>(.*)\<\/td\>/s",$str,$title);

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

 s (PCRE_DOTALL)

この修飾子が設定されている場合、パターン内のドット メタ文字は、改行を含むすべての文字に一致します。それがない場合、改行は除外されます。この修飾子は、Perl の /s 修飾子と同等です。[^a] などの否定的なクラスは、この修飾子の設定に関係なく、常に改行文字に一致します。

于 2013-03-24T10:56:21.963 に答える