Php parses the html class library simple_html_dom (details ). : Github. comsamacssimple_html_dom parser not only helps us verify html documents, but also resolves html documents that do not comply with W3C standards. It uses a https://github.com/samacs/simple_html_dom like jQuer
The parser not only helps us verify html documents, but also resolves html documents that do not comply with W3C standards. It uses element selectors similar to jQuery to locate and locate elements by id, class, and tag. It also provides the function of adding, deleting, and modifying document trees. Of course, such a powerful html Dom parser is not perfect. you need to be very careful about memory consumption during use. But don't worry. in this article, I will introduce how to avoid excessive memory consumption.
Start to use
After uploading a class file, you can call this class in three ways:
Load html documents from URLs
Load html documents from strings
Load html documents from files
The code is as follows:
// Create a Dom instance
$ Html = new simple_html_dom ();
// Load from the url
$ Html-> load_file ('http: // www.jb51.net ');
// Load from a string
$ Html-> load ('Loading html documents from strings');
// Load from a file
$ Html-> load_file ('path/file/test.html ');
?>
If you load html documents from strings, you must first download them from the network. We recommend that you use cURL to capture html documents and load them into the DOM.
Search for html elements
You can use the find function to find elements in html documents. The returned result is an array containing objects. We use functions in the html dom parsing class to access these objects. The following is an example:
The code is as follows:
// Search for hyperlink elements in html documents
$ A = $ html-> find ('A ');
// Search for the N hyperlink in the document. if N is not found, an empty array is returned.
$ A = $ html-> find ('A', 0 );
// Find the p element whose id is main
$ Main = $ html-> find ('P [id = main] ', 0 );
// Search for all p elements containing the id attribute
$ Ps = $ html-> find ('P [id] ');
// Search for all elements with the id attribute
$ Ps = $ html-> find ('[id]');
?>
You can also use a selector similar to jQuery to find the positioning element:
The code is as follows:
// Find the element whose id is '# INER'
$ Ret = $ html-> find ('# INER ');
// Find all elements of class = foo
$ Ret = $ html-> find ('. Foo ');
// Search for multiple html tags
$ Ret = $ html-> find ('a, img ');
// It can also be used like this
$ Ret = $ html-> find ('a [title], img [title] ');
?>
The parser supports searching child elements.
The code is as follows:
// Find All li items in the ul list
$ Ret = $ html-> find ('Ul li ');
// Find the li item of the specified class = selected in the ul list
$ Ret = $ html-> find ('Ul li. selected ');
?>
If you think this is difficult to use, you can use built-in functions to easily locate the parent element, child element, and adjacent element of an element.
The code is as follows:
// Returns the parent element.
$ E-> parent;
// Returns the array of child elements.
$ E-> children;
// Return the specified child element through the index number
$ E-> children (0 );
// Returns the first resource speed
$ E-> first_child ();
// Returns the last child element.
$ E-> last _ child ();
// Returns the last adjacent element.
$ E-> prev_sibling ();
// Returns the next adjacent element.
$ E-> next_sibling ();
?>
Element attribute operations
Use a simple regular expression to operate the attribute selector.
[Attribute]-select an html element containing an attribute
[Attribute = value]-select all html elements of the specified value attribute
[Attribute! = Value]-select all html elements with unspecified value attributes
[Attribute ^ = value]-select all html elements starting with the specified value
[Attribute $ = value] Select All html elements of the end attribute of the specified value
[Attribute * = value]-select all html elements that contain the specified value attribute
Call element attributes in the parser
Element attributes in the DOM are also objects:
The code is as follows:
// In this example, assign the $ a anchor value to the $ link variable.
$ Link = $ a-> href;
?>
Or:
The code is as follows:
$ Link = $ html-> find ('A', 0)-> href;
?
Each object has four basic object attributes:
Tag-returned html tag name
Innertext-return innerHTML
Outertext-return outerHTML
Plaintext-return the text in the html tag
Edit element in Parser
The usage of editing element attributes is similar to calling them:
The code is as follows:
// Assign a new value to the $ a anchor link
$ A-> href = 'http: // www.jb51.net ';
// Delete the anchor
$ A-> href = null;
// Check whether there is an anchor link
If (isset ($ a-> href )){
// Code
}
?>
The parser does not have a special method to add or delete elements, but you can use it as a work und:
The code is as follows:
// Encapsulate elements
$ E-> outertext ='
'. $ E-> outertext .'
';
// Delete an element
$ E-> outertext = '';
// Add element
$ E-> outertext = $ e-> outertext .'
Foo
';
// Insert element
$ E-> outertext ='
Foo
'. $ E-> outertext;
?
Saving the modified html DOM document is also very simple:
The code is as follows:
$ Doc = $ html;
// Output
Echo $ doc;
?>
How to avoid excessive memory consumption by the parser
In the beginning of this article, I mentioned the problem that the Simple HTML DOM parser consumes too much memory. If the php script occupies too much memory, the website will stop responding and other serious problems. The solution is also very simple. after the parser loads the html document and uses it, remember to clear this object. Of course, do not take the problem too seriously. If only two or three documents are loaded, there are no different regions to clean up or not clean up. When you load 5 or more documents, it is absolutely your responsibility to clean up the memory when you use up one. ^_^
The code is as follows:
$ Html-> clear ();
?>
The http://www.bkjia.com/PHPjc/327997.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327997.htmlTechArticle:https://github.com/samacs/simple_html_dom parser not only helps us validate html documents, but also resolves html documents that do not comply with W3C standards. It uses something similar to jQuer...