tooyoungtoosimple simplehtmldom Doc api幫助文檔
Jul 29, 2016 am 08:48 AMAPI 參考
幫助函數
object str_get_html ( string $content ) 從字串建立 DOM 物件。
object file_get_html ( string $filename ) 從檔案或 URL 建立 DOM 物件。
DOM 方法與屬性
stringplaintext 傳回從 HTML 擷取的內容。
voidclear() 清理記憶體。
voidload ( string $content ) 從字串中載入內容。
stringsave ( [string $filename] ) 將內部 DOM 樹轉儲回字串。如果設定了$filename,結果字串將會儲存到檔案中。
voidload_file ( string $filename ) 從檔案或 URL 載入內容。
voidset_callback ( string $function_name ) 設定回呼函數。
mixedfind ( string $selector [, int $index] ) 透過 CSS 選擇器找出元素。如果設定了索引,則傳回第 N 個元素對象,否則傳回對象數組。
元素方法與屬性
string[attribute] 讀取或寫入元素的屬性值。
stringtag 讀取或寫入元素的標籤名稱。
stringoutertext 讀取或寫入元素的外部 HTML 文字。
stringinnertext 讀取或寫入元素的內部 HTML 文字。
stringplaintext 讀取或寫入元素的純文字。
mixedfind ( string $selector [, int $index] ) 透過 CSS 選擇器找出子項目。如果設定了index,則傳回第N個元素對象,否則傳回一個物件陣列。
DOM 遍歷
mixed$e->children ( [int $index] ) 如果設定了 index,則傳回第 N 個子對象,否則傳回子物件陣列。
element$e->parent () 傳回元素的父元素。
element$e->first_child () 傳回 element 的第一個子元素,如果沒有找到則傳回 null。
element$e->last_child () 傳回 element 的最後一個子元素,如果找不到則傳回 null。
element$e->next_sibling () 傳回 element 的下一個同級元素,如果沒有找到則傳回 null。
element$e->prev_sibling () 傳回 element 的上一個同級元素,如果沒有找到則傳回 null。
Camel 命名轉換您也可以使用 W3C STANDARD Camel 命名轉換來呼叫方法。
string$e->getAttribute ( $name ) string$e->attribute
void$e->setAttribute ( $name, $value ) void$value = $e->attribute
bool$e->attribute ; $name ) boolisset($e->attribute)
void$e->removeAttribute ( $name ) void$e->attribute = null
element$e->getElementById ( $id ) mix$ e->find ( "# $id", 0 )
mixed$e->getElementsById ( $id [,$index] ) mixed$e->find ( "#$id" [, int $index] )
element$e->getElementByTagName ($ name ) 混合$e->find ( $name, 0 )
mixed$e->getElementsByTagName ( $name [, $index] ) 混合$e->find ( $name [, int $index] )
element$e ->parentNode () element$e->parent ()
mixed$e->childNodes ( [$index] ) mixed$e->children ( [int $index] )
element$e->firstChild () element$ld () element$ e->first_child ()
element$e->lastChild () element$e->last_child ()
element$e- >nextSibling () element$e->next_sibling ()
element$e->previousSibling ()
element$e->previousSibling () ) $e->prev_sibling ()
// 從字串建立DOM 物件
$html = str_get_html('
// 從URL 建立DOM物件
$html = file_get_html('http://www.google.com/');
// 從HTML 檔案建立DOM 物件
$html = file_get_html('test.htm');
// 建立DOM 物件
$html = new simple_html_dom();
// 從字串載入HTML
$html->load('Hello!');
// 從URL 載入HTML
$html->load_file('http://www.google.com/');
// 從HTML 檔案載入HTML
$html->load_file('test.htm');
// 找出所有錨點,返回元素物件數組
$ret = $html->find('a');
// 查找(N)thachor,如果沒有找到則返回元素物件或null(從零開始)
$ret = $html- >find('a', 0);
// 找出所有
$ret = $html->find('div[id=foo]');
// 找出所有;帶有id 屬性
$ret = $html->find('div[id]');
// 找出所有具有屬性id 的元素
$ret = $html->find('[id]' );
// 找出所有id=foo 的元素
$ret = $html->find('#foo');
// 找出所有class=foo 的元素
$ret = $html->find('. foo');
// 找出所有錨點和圖像
$ret = $html->find('a, img');
// 找出所有帶有「title」屬性的錨點和圖片
$ret = $html->find('a[title], img[title]');
// 查找所有
- 中
$es = $html->find('ul li');
// 尋找巢狀的
$es = $html->find('div div div'); 🎜// Find all <td> in <table> which class=hello
$es = $html->find('table.hello td');
// Find all td tags with attribite align=center in table tags
$es = $html->find(''table td[align=center]');
// Find all <li> in <ul>
foreach($html->find('ul') as $ul)
{
foreach($ul->find('li') as $li)
{
// do something...
}
}
// Find first <li> in first <ul>
$e = $html->find('ul', 0)->find('li', 0);
Supports these operators in attribute selectors:
[attribute] Matches elements that have the specified attribute.
[attribute=value] Matches elements that have the specified attribute with a certain value.
[attribute!=value] Matches elements that don't have the specified attribute with a certain value.
[attribute^=value] Matches elements that have the specified attribute and it starts with a certain value.
[attribute$=value] Matches elements that have the specified attribute and it ends with a certain value.
[attribute*=value] Matches elements that have the specified attribute and it contains a certain value.
// Find all text blocks
$es = $html->find('text');
// Find all comment (<!--...-->) blocks
$es = $html->find('comment');
// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;
// Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
$e->href = 'my link';
// Remove a attribute, set it's value as null!
$e->href = null;
// Determine whether a attribute exist?
if(isset($e->href))
echo 'href exist!';
// Example
$html = str_get_html("<div>foo <b>bar</b></div>");
$e = $html->find("div", 0);
echo $e->tag; // Returns: " div"
echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
echo $e->innertext; // 回傳:" foo bar"
echo $e->plaintext; // 回傳:" foo bar"
$e->; tag 讀取或寫入元素的標籤名稱。純文字。
// 刪除一個元素,將其設為空字串
$e->outertext = ''; // 追加一個元素
$e- >outertext = $e->outertext . '
$e->outertext = '
// 如果您不太熟悉HTML DOM,請查看此連結以了解更多信息...
// 範例
echo $html->find("#div1", 0) ->children(1)->children(1)->children(2)->id;
///
echo 或
echo $html->getElementById("div1")->childNodes(1) ->childNodes(1)->childNodes(2)->getAttribute('id');
也可以使用Camel 命名轉換來呼叫方法。 $e->children ( [int $index] )如果設定了索引,則傳回第N 個子對象,否則傳回子物件陣列。 >first_child () 傳回元素的第一個子物件。 ->next_sibling () 傳回元素的下一個兄弟元素,如果找不到,則傳回null。
// 將內部DOM 樹轉儲回字串
$str = $html;回顯$html;
// 將內部DOM 樹轉儲回字串
$str = $html->save();
/ / 將內部DOM 樹轉儲回檔案中
$html->save('result.htm');
// 寫一個參數為「$element」的函式
function my_callback($element) {
// 隱藏所有;標籤
if ($element->tag=='b')
$element->outertext = '';
}
// 用函數名稱註冊回呼函數
$html->set_callback('my_callback' );
// 轉儲時會呼叫回呼函數
echo $html;
以上就介紹了tooyoungtoosimple simplehtmldom Doc api幫助文檔,包括tooyoungtoosimple方面的內容,希望對PHP教程有興趣的朋友有所幫助。

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)