PHP code example of webignition / html-document-link-finder

1. Go to this page and download the library: Download webignition/html-document-link-finder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webignition / html-document-link-finder example snippets


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);

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

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');