簡體   English   中英

需要在RegExp下更正

[英]Need correction in below RegExp

我想從html tags獲取類似產品標題的頁面中的特定數據

以下是我從網站獲得的div代碼-

    <div class="pdct-inf">
    <h2 class="h6" style="min-height:38px;height:38px;">
<a id="ctl00_cphMain_rPdctG_ctl01_hTitle" href="/whirlpool-whirlpool-direct-drive-285753a-ap3963893.html">Whirlpool Direct Drive Washer Mot...</a></h2><div class="startext">
<div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating" style="cursor:pointer; float:left; text-align:right;" class="page-style-stars-web-sm rating-5"></div>
<meta itemprop="worstRating" content="1"><meta itemprop="bestRating" content="5"><meta itemprop="ratingValue" content="5">&nbsp;(<a href="/whirlpool-whirlpool-direct-drive-285753a-ap3963893.html#diy">434</a>)
    </div>
    </div>

我想在<a>之間獲取此文本Whirlpool Direct Drive Washer Mot...

以下是我的PHP代碼-

<?php

$html = file_get_contents("http://www.programminghelp.com/");

preg_match_all(
    '/<h2><a href="(.*?)" rel="bookmark" title=".*?">(.*?)<\/a><\/h2>/s',
    $html,
    $posts, // will contain the article data
    PREG_SET_ORDER // formats data into an array of posts
);

foreach ($posts as $post) {
    $link = $post[1];
    $title = $post[2];

    echo $title . "\n";
}

echo "<p>" . count($posts) . " product found</p>\n";

?>

我需要幫助為上述div內容編寫regexp。

preg_match_all(
        '/<h2><a href="(.*?)" rel="bookmark" title=".*?">(.*?)<\/a><\/h2>/s',

也許像這樣的HTML / XML解析器會更合適。 (正則表達式不適用於注釋中所述的[X] HTML解析)

如果您想為此使用正則表達式,可以嘗試使用類似這樣的東西

/<h2.*>\s*<a.* href="(.*)">(.*)<\/a>/m

您可以在此php沙箱中看到它與您的示例一起使用。

暫無
暫無

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

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