2014-09-04 26 views

回答

8

這取決於你的意思是「避免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; 
+1

我只想要回111111.How實現 – 2014-09-04 10:16:04

+0

?儘管我無法保證它可以正常工作,但可以使用POSSIBLE修補程序進行編輯。我沒有SHD文檔可以使用並在 – Joe 2014-09-04 10:23:57

+0

上測試它已經工作了。感謝 – 2014-09-04 10:56:03

0
$wordlist = array("<p>", "</p>") 

foreach($wordlist as $word) 
    $string = str_replace($word, "", $string); 
+2

祝您好運添加每個可能的HTML標籤到wordlist中:-) – Joe 2014-09-04 10:18:13

4

如果你的文字總是在相同的位置,試試這個:

$html->find('text', 2)->plaintext; // should return 111111 
+0

根據手冊(http://simplehtmldom.sourceforge.net/manual .htm)中的「如何訪問HTML元素的屬性?」部分和「魔術屬性」選項卡,這將返回內部標籤的內容,只是實際標籤本身 – Joe 2014-09-04 10:25:41

+0

@Joe是,而'2'索引指定第三個元素是'111111' ... – Enissay 2014-09-04 10:27:30

+0

啊! , 我懂了。足夠公平:-) – Joe 2014-09-04 10:28:33

1

這裏是我的解決方案

enter image description here

我只想要主文本部分。

$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 = "":""; 

Official Documentation

相關問題