2012-07-05 105 views
1

我需要訪問div中沒有​​任何標籤的48.20 Lac(s)文本,這是我無法訪問它的原因。 我需要在PHP文件中找到它。我嘗試了$ html-> find('div.priceDetail'),接着修剪(strip_tags($ result)),這給了我48.20 Lac(s)+不需要的文本。 因爲我要構建一個通用文件,所以我不能依賴於特定的固定情況的爆炸和內爆。查找在div元素中沒有任何標籤的文本

<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> 

回答

4

這裏用的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第一圖像之後。

+0

賓果!!!!像一個魅力工作 – user1425322 2012-07-05 09:54:38

5

與文字的隨機載荷離開的時候做大量的工作,你可以用一個DOM解析器,然後,拉位出你想要這個正則表達式:

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

結果

48.20 Lac(s) 

(將RegEx中的5更改爲小數點前要允許的位數)

+0

非常感謝... – user1425322 2012-07-05 09:55:05

相關問題