我使用PHP簡單的Html Dom得到一些HTML,現在我有一個HTML DOM像跟隨代碼,我需要獲取純文本內部的div,但避免p標籤和他們的內容(只返回111111),誰可以幫助我?提前致謝!PHP簡單的Html Dom獲取div的純文本,但避免所有其他標記
<div>
<p>00000000</p>
111111
<p>22222222</p>
</div>
我使用PHP簡單的Html Dom得到一些HTML,現在我有一個HTML DOM像跟隨代碼,我需要獲取純文本內部的div,但避免p標籤和他們的內容(只返回111111),誰可以幫助我?提前致謝!PHP簡單的Html Dom獲取div的純文本,但避免所有其他標記
<div>
<p>00000000</p>
111111
<p>22222222</p>
</div>
這取決於你的意思是「避免p標籤」。
如果您只是想刪除標籤,那麼只需運行strip_tags()
就可以滿足您的需求。
如果您確實想要返回「11111」(即剝離標籤及其內容),那麼這不是一個可行的解決方案。對於這一點,這樣的事情可能工作:
$myDiv = $html->find('div'); // wherever your the div you're ending up with is
$children = $myDiv->children; // get an array of children
foreach ($children AS $child) {
$child->outertext = ''; // This removes the element, but MAY NOT remove it from the original $myDiv
}
echo $myDiv->innertext;
$wordlist = array("<p>", "</p>")
foreach($wordlist as $word)
$string = str_replace($word, "", $string);
祝您好運添加每個可能的HTML標籤到wordlist中:-) – Joe 2014-09-04 10:18:13
如果你的文字總是在相同的位置,試試這個:
$html->find('text', 2)->plaintext; // should return 111111
這裏是我的解決方案
我只想要主文本部分。
$title_obj = $article->find(".ofr-descptxt",0); //Store the Original Tree ie) h3 tag
$title_obj->children(0)->outertext = ""; //Unset <br/>
$title_obj->children(1)->outertext = ""; //Unset the last Span
echo $title_obj; //It has only first element
編輯: 如果你有PHP錯誤 嘗試圍成的if else或者試試我懶人代碼
($title_obj->children(0))?$title_obj->children(0)->outertext="":"";
($title_obj->children(1))?$title_obj->children(1)->outertext = "":"";
我只想要回111111.How實現 – 2014-09-04 10:16:04
?儘管我無法保證它可以正常工作,但可以使用POSSIBLE修補程序進行編輯。我沒有SHD文檔可以使用並在 – Joe 2014-09-04 10:23:57
上測試它已經工作了。感謝 – 2014-09-04 10:56:03