Php 查找div元素中没有任何标记的文本
我需要访问48.20 Lac(s)文本,该文本在div中没有任何标记,这就是我无法访问它的原因。 我需要在一个PHP文件中找到它。我尝试了$html->find('div.priceDetail'),然后是trim(strip_tags($result)),这给了我48.20 Lac(s)+不必要的文本。 因为我必须构建一个通用文件,所以对于一个特定的固定案例,我不能依赖于爆炸和内爆Php 查找div元素中没有任何标记的文本,php,parsing,html,simple-html-dom,Php,Parsing,Html,Simple Html Dom,我需要访问48.20 Lac(s)文本,该文本在div中没有任何标记,这就是我无法访问它的原因。 我需要在一个PHP文件中找到它。我尝试了$html->find('div.priceDetail'),然后是trim(strip_tags($result)),这给了我48.20 Lac(s)+不必要的文本。 因为我必须构建一个通用文件,所以对于一个特定的固定案例,我不能依赖于爆炸和内爆 <div class="priceDetail"> <b>Total Price
<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>
总价:
48.20拉丁美洲和加勒比海(s)
(每平方英尺价格:3679卢比)
4%
每平方英尺。这个房产的价格是
根据MagicBricks.com,高于该地区的平均房价
价格趋势。
想知道更多关于这件事
使用DOM解析器尽可能多地工作,然后在随机加载文本时,使用此正则表达式提取所需的位:
([0-9]{1,5}?\.[0-9]{2} Lac\(s\))
结果
48.20 Lac(s)
(将正则表达式中的5更改为小数点之前允许的位数)使用DOM解析器尽可能多地工作,然后在随机加载文本时,使用此正则表达式拉出所需的位:
([0-9]{1,5}?\.[0-9]{2} Lac\(s\))
结果
48.20 Lac(s)
(将正则表达式中的5更改为小数点前允许的位数)这里有一个使用DomDocument的解决方案,可能比正则表达式更健壮:
$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中第一个图像之后。这里有一个使用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中第一个图像之后