webignition / html-document-link-finder
Find anchor URLs in a given HTML document
Installs: 6 735
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.2.0
- webignition/absolute-url-deriver: >=3,<4
- webignition/uri: >=0.1,<1
- webignition/web-page-model: >=5.7,<6
Requires (Dev)
- mockery/mockery: ^1
- phpstan/phpstan: ^0.11.5
- phpstan/phpstan-mockery: ^0.11.0
- phpunit/phpunit: ^7
- squizlabs/php_codesniffer: ^3
README
Get a collection of absolute urls, with their associated elements, for links in a HTML document.
Usage
Getting a LinkCollection from a WebPage
use webignition\HtmlDocumentLinkUrlFinder\HtmlDocumentLinkUrlFinder; use webignition\WebResource\WebPage\WebPage; $webPageUrl = 'http://www.google.co.uk/search?q=Hello+World'; $webPage = WebPage::createFromContent((string) file_get_contents($sourceUrl)); $finder = new HtmlDocumentLinkUrlFinder(); $linkCollection = $finder->getLinkCollection($webPage, $webPageUrl);
Accessing a LinkCollection
use Psr\Http\Message\UriInterface; // Assuming $linkCollection from previous example // Iterating foreach ($linkCollection as $link) { $link->getUri(); // UriInterface instance $link->getElement(); // \DOMElement instance } // Counting count($linkCollection); // Get URIs only $linkCollection->getUris(); // array of UriInterface // Get unique URIs only $linkCollection->getUniqueUris(); // array of UriInterface
Filtering a LinkCollection
All LinkCollection::filterBy*()
methods return a new LinkCollection
instance.
use webignition\Uri\ScopeComparer; // Filtering $anchorLinks = $linkCollection->filterByElementName('a'); $elementsWithRelStylesheetAttribute = $linkCollection->filterByAttribute('rel', 'stylesheet'); $linksWithinUrlScope = $linkCollection->filterByUrlScope( new ScopeComparer(), ['http://example.com/'] ); $linkElementsWithRelStylesheetAttribute = $linkCollection ->filterByElementName('link') ->filterByAttribute('rel', 'stylesheet');