First things first, so require the package:
composer require spresnac/laravel-url-helper
Every time you need to get URLs from some kind of Text or HTML, this class is a helping hand for you.
- Make an instance of the class
- Use the constructor or the
setContent
method to put in your content as string. - Get your URLs by using one of the following methods.
This will just the content you put in via constructor or setContent
method.
You will retrieve a Collection with only the URLs inside a href
$content_helper = new ContentHelper('insert HTML here');
$result = $content_helper->getHrefs();
// $result is an Collection with all the hrefs in it.
You will retrieve an array with only the URLs inside a href
$content_helper = new ContentHelper('insert HTML here');
$result = $content_helper->getHrefsAsArray();
// $result is an Collection with all the hrefs in it.
You will retrieve a collection of urls from src
sources.
$content_helper = new ContentHelper('insert HTML here');
$result = $content_helper->getSrcUrls();
// $result is an Collection with all the src-urls in it.
This method will try to find all the urls in a plaintext string.
$content_helper = new ContentHelper('insert some plaintext here');
$result = $content_helper->getLinksFromPlaintext();
// $result is an Collection with all the urls from the plaintext in it.
This will execute getHrefs
, getSrcUrls
and getLinksFromPlaintext
and will return a uniqueified collection of all the urls.
$content_helper = new ContentHelper('insert some html or plaintext here');
$result = $content_helper->getAllTheLinks();
// $result is an Collection with all the urls.
Some useful helper functions to handle URL related manipulating or info-gatherings.
Normalizes URL with .. in it and leaves the all oher URL "as is".
$url_helper = new URLHelper();
$normalized_url = $url_helper->normalize('https://example.com/my/dir/with/two/../init.html');
// $normalized_url is now https://example.com/my/dir/with/init.html
Returns false
in case of error
This is the missing opposite of parse_url
to rebuild a url from its parts.
$url_helper = new URLHelper();
$parsed_url = parse_url('https://example.com/test/url.php');
$my_url = $url_helper->build_url($parsed_url);
This will return the "Main Domain" part of an URL. The "Main Domain" is like the SLD + TLD domain part.
$url_helper = new URLHelper();
$main_domain_part = $url_helper->getMainDomainPart('https://www3.example.com/some/url');
// $main_domain_part is now "example.com"
This will return the Subdomain part of an URL.
$url_helper = new URLHelper();
$sub_domain_part = $url_helper->getMainDomainPart('https://www3.brick.example.com/some/url');
// $sub_domain_part is now "www3.brick"
Handling with web-sitemaps can be handy with some helpers at your side.
Will return all the urls inside a sitemap as Collection
$sitemap_helper = new SitemapHelper();
$urls = $sitemap_helper->process_input_from_string('content_of_your_sitemap_as_string');
// $urls are a collection of all the urls found
Returns the urls in a sitemap url as Collection
$sitemap_helper = new SitemapHelper();
$urls = $sitemap_helper->process_input_from_url('url_to_your_sitemap');
// $urls are a collection of all the urls found
Simply run
composer test-ci
or
vendor/bin/phpunit
Be productive 😉