簡體   English   中英

PHP中的正則表達式以搜索特定數據集

[英]regular expression in php to search particular set of data

搜索我想從我的網站中提取一段。 它們是索引頁面中使用的20多個段落標簽。 關鍵區別 在每個標簽中,style18類使用1次,style 19類使用3次。 我想用內容操作系統類18搜索它們。 主要內容


<p class="margin">
    <span class="style18">*the main content*</span>
      » <a href="https://example1.html">
        somthing</a>

        <span class="style19">[somthing]</span>
         » <a href="https://example1.html">Town</a>

         <span class="style19">[somthing]</span>
          » <a href="https://example1.html">somthing</a>

    <span class="style19">[somthing]</span> »
    <a href="https://www.example.html">somthing</a>

    <span class="style19">[somthing]</span>

</p>

<?php
  $data = file_get_contents('https://www.example.net/index.php');

  preg_match('/<title>([^<]+)<\/title>/i', $data, $matches);
  $title = $matches[1];

  echo preg_match('/(<p)\s.+\n.+(style18).+Single\sTrack(.+)\n(.+)\n(.+)\n(.+)\n.+(style19).+\n(.+)\n(.+)\n.+(style19).+\n(.+)\n(.+)\n.+(style19).+\n(.+)\n(.+)\n.+(style19).+\n\n<\/p>/i', $data, $matches);

  $img = $matches[1];

  echo $title."<br>\n";
  echo $img;
  ?>

歡迎來到@Aerro社區。

如果我正確地回答了您的問題,則要提取具有特定規則的其他跨度所包圍的任何跨度的內部內容。 盡管這很容易用regexp折斷手指,但是像XPath這樣的(樹/圖)查詢語言將是解決此問題的好方法。

看看例如http://php.net/manual/en/simplexmlelement.xpath.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM