在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:paquettg/php-html-parser开源软件地址:https://github.com/paquettg/php-html-parser开源编程语言:HTML 69.3%开源软件介绍:PHP Html ParserPHPHtmlParser is a simple, flexible, html parser which allows you to select tags using any css selector, like jQuery. The goal is to assist in the development of tools which require a quick, easy way to scrap html, whether it's valid or not! InstallInstall the latest version using composer. $ composer require paquettg/php-html-parser This package can be found on packagist and is best loaded using composer. We support php 7.2, 7.3, and 7.4. Basic UsageYou can find many examples of how to use the DOM parser and any of its parts (which you will most likely never touch) in the tests directory. The tests are done using PHPUnit and are very small, a few lines each, and are a great place to start. Given that, I'll still be showing a few examples of how the package should be used. The following example is a very simplistic usage of the package. // Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
$a = $dom->find('a')[0];
echo $a->text; // "click here" The above will output "click here". Simple, no? There are many ways to get the same result from the DOM, such as Support PHP Html Parser FinanciallyGet supported Monolog and help fund the project with the Tidelift Subscription. Tidelift delivers commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Loading FilesYou may also seamlessly load a file into the DOM instead of a string, which is much more convenient and is how I expect most developers will be loading the HTML. The following example is taken from our test and uses the "big.html" file found there. // Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadFromFile('tests/data/big.html');
$contents = $dom->find('.content-border');
echo count($contents); // 10
foreach ($contents as $content)
{
// get the class attr
$class = $content->getAttribute('class');
// do something with the html
$html = $content->innerHtml;
// or refine the find some more
$child = $content->firstChild();
$sibling = $child->nextSibling();
} This example loads the html from big.html, a real page found online, and gets all the content-border classes to process. It also shows a few things you can do with a node but it is not an exhaustive list of the methods that a node has available. Loading URLsLoading a URL is very similar to the way you would load the HTML from a file. // Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadFromUrl('http://google.com');
$html = $dom->outerHtml;
// or
$dom->loadFromUrl('http://google.com');
$html = $dom->outerHtml; // same result as the first example loadFromUrl will, by default, use an implementation of the // Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
use App\Services\MyClient;
$dom = new Dom;
$dom->loadFromUrl('http://google.com', null, new MyClient());
$html = $dom->outerHtml; As long as the client object implements the interface properly, it will use that object to get the content of the url. Loading StringsLoading a string directly is also easily done. // Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<html>String</html>');
$html = $dom->outerHtml; OptionsYou can also set parsing option that will effect the behavior of the parsing engine. You can set a global option array using the // Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
use PHPHtmlParser\Options;
$dom = new Dom;
$dom->setOptions(
// this is set as the global option level.
(new Options())
->setStrict(true)
);
$dom->loadFromUrl('http://google.com',
(new Options())->setWhitespaceTextNode(false) // only applies to this load.
);
$dom->loadFromUrl('http://gmail.com'); // will not have whitespaceTextNode set to false. At the moment we support 12 options. Strict Strict, by default false, will throw a whitespaceTextNode The whitespaceTextNode, by default true, option tells the parser to save textnodes even if the content of the node is empty (only whitespace). Setting it to false will ignore all whitespace only text node found in the document. enforceEncoding The enforceEncoding, by default null, option will enforce an character set to be used for reading the content and returning the content in that encoding. Setting it to null will trigger an attempt to figure out the encoding from within the content of the string given instead. cleanupInput Set this to removeScripts Set this to removeStyles Set this to preserveLineBreaks Preserves Line Breaks if set to removeDoubleSpace Set this to removeSmartyScripts Set this to htmlSpecialCharsDecode By default this is set to selfClosing This option contains an array of all self closing tags. These tags must be self closing and the parser will force them to be so if you have strict turned on. You can update this list with any additional tags that can be used as a self closing tag when using strict. You can also remove tags from this array or clear it out completly. noSlash This option contains an array of all tags that can not be self closing. The list starts off as empty but you can add elements as you wish. Static FacadeYou can also mount a static facade for the Dom object. PHPHtmlParser\StaticDom::mount();
Dom::loadFromFile('tests/big.hmtl');
$objects = Dom::find('.content-border'); The above php block does the same find and load as the first example but it is done using the static facade, which supports all public methods found in the Dom object. Modifying The DomYou can always modify the dom that was created from any loading method. To change the attribute of any node you can just call the use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
$a = $dom->find('a')[0];
$a->setAttribute('class', 'foo');
echo $a->getAttribute('class'); // "foo" You may also get the use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
/** @var Dom\Node\AbstractNode $a */
$a = $dom->find('a')[0];
$tag = $a->getTag();
$tag->setAttribute('class', 'foo');
echo $a->getAttribute('class'); // "foo" It is also possible to remove a node from the tree. Simply call the use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
/** @var Dom\Node\AbstractNode $a */
$a = $dom->find('a')[0];
$a->delete();
unset($a);
echo $dom; // '<div class="all"><p>Hey bro, <br /> :)</p></div>'); You can modify the text of use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
/** @var Dom\Node\InnerNode $a */
$a = $dom->find('a')[0];
$a->firstChild()->setText('biz baz');
echo $dom; // '<div class="all"><p>Hey bro, <a href="google.com">biz baz</a><br /> :)</p></div>' |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论