在div元素中查找没有任何标记的文本

时间:2012-07-05 08:57:32

标签: php parsing html simple-html-dom

我需要访问div中没有​​任何标记的48.20 Lac(s)文本,这就是我无法访问它的原因。 我需要在PHP文件中找到它。我已经尝试了$ html-> find('div.priceDetail'),然后是trim(strip_tags($ result)),它给了我48.20 Lac(s)+不需要的文本。 由于我要构建一个通用文件,所以我不能依赖于针对特定固定案例的爆炸和爆炸。

<div class="priceDetail">
    <b>Total Price :</b>
    <img alt="" src="someimage">48.20 Lac(s)
    <!-- Per Sq Ft Price -->
    <span class="pricePerSqFt">(Price per sq.ft. : Rs. 3,679)</span>
    <!-- Code for price Trends -->
    <span class="priceGrowth">4 %
        <img alt="" src="someimage"
        align="absmiddle">
        <span class="iconWhatisThis">
            <img src="someimage"
            class="whatIcon" align="absmiddle">
            <span style="" id="StoolTip" class="price_main-c"></span>
        </span>
    </span>
    <div class="tt_top-c">
        <span class="priceGrowth"></span>
    </div>
    <div class="tt_mid-c">
        <div class="tt_pointer-c"></div>
        <div>
            <span class="tt_txt-c">Per sq.ft. price for this property is
                <b>higher than the average</b>property price in this locality as per MagicBricks.com
                Price Trends.</span>
        </div>
        <span class="tt_txt-c">
            <span class="tp_txt">To know more about this
                <a href="#priceTrends" onclick="swithTab('priceTrends', tabbedDivArray);">Click
Here</a>
            </span>
        </span>
    </div>
    <div class="tt_bot-c"></div>
</div>

2 个答案:

答案 0 :(得分:5)

使用DOM Parser做尽可能多的工作,然后当你随机加载文本时,用这个RegEx拉出你想要的东西:

([0-9]{1,5}?\.[0-9]{2} Lac\(s\))

结果

48.20 Lac(s)

(将RegEx中的5更改为小数点前要允许的位数)

答案 1 :(得分:4)

这是一个使用DomDocument的解决方案,可能比Regex更强大:

$DOM = new DOMDocument;
$DOM->loadHTML($str);

//Get all the image tags
$elem = $DOM->getElementsByTagName('img');
//Get the first Image
$first = $elem->item(0);
//Get the node after the image
$txt=  $first->nextSibling;
//Get the text
echo $txt->nodeValue;

当然,它要求文本始终位于div中的第一个图像之后。