在div元素中查找没有任何标签的文字 [英] Find text without any tag in a div element
问题描述
我需要访问div中没有任何标签的48.20 Lac(s)文本,这是我无法访问它的原因。
我需要在PHP文件中找到这个。我试过$ html-> find('div.priceDetail'),然后是trim(strip_tags($ result)),这给了我48.20 Lac(s)+无需文本。
因为我要构建一个通用文件,所以我不能依赖于特定的固定情况的爆炸和内爆。
I need to access the 48.20 Lac(s) text which is without any tag inside the div,which is the reason I'm not able to access it. I need to find this in a PHP file.I've tried $html->find('div.priceDetail') followed with trim(strip_tags($result)) which gave me 48.20 Lac(s) + unnecesary text. Since I've to build a generic file I can't depend on exploding and imploding for a particular fixed case.
<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>
推荐答案
这里有一个DomDocument解决方案,可能比Regex更强大:
Here a solution with DomDocument, probably more robust than 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中的第一个图像之后。
Of course it requires that the text is always located after the first image in the div.
这篇关于在div元素中查找没有任何标签的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!