aammui / laravel-parser
Laravel/PHP 的 HTML 解析器
v1.0.1
2021-11-03 03:11 UTC
Requires
- php: ^7.4|^8.0
- ext-dom: *
- symfony/dom-crawler: ^5.3
Requires (Dev)
- brianium/paratest: ^6.3
- friendsofphp/php-cs-fixer: ^3.0
- nunomaduro/collision: ^5.10
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^4.7
This package is auto-updated.
Last update: 2024-09-29 05:57:01 UTC
README
Laravel/PHP 的 HTML 解析器。
简介
此包解析 HTML 响应,具有非常高效的 API。但它不解析任何 URL。
此包是 https://github.com/symfony/dom-crawler 的 Laravel 封装。
安装
composer require aammui/laravel-parser
使用
use Aammui\LaravelParser\Facade\PHPSoup; $html = <<<END <html lang="en"> <head> <title> The Dormouse's story </title> </head> <body> <p class="title"> <b> The Dormouse's story </b> </p> <p class="story"> Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"> Elsie </a> , <a class="sister" href="http://example.com/lacie" id="link2"> Lacie </a> and <a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p class="story"> ... </p> </body> </html> END; $soup = PHPSoup::Parse($html); // Outputs: <title>The Dormouse's story</title> echo $soup->get('title')->first()->outerHtml(); // Outputs: The Dormouse's story echo $soup->get('title')->first()->text(); // Output: echo $soup->get('p')->first()->attributes("href"); // [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, // <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, // <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] echo $soup->get("a"); // Search by attributes echo $soup->get("li[data-item]")->outerHtml(); echo $soup->get("li[data-item='2']")->outerHtml(); // Search by Css echo $soup->get("head > title")->outerHtml(); # [<title>The Dormouse's story</title>] // Find the siblings of tags: echo $soup->get("#link1 ~ .sister")->outerHtml(); # [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]