spresnac / laravel-url-helper
Helping you with URL related problems
Installs: 1 073
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-curl: *
- ext-simplexml: *
Requires (Dev)
- illuminate/support: ^8|^9|^10
- phpunit/phpunit: ^9.5.14
README
Content:
Installation
First things first, so require the package:
composer require spresnac/laravel-url-helper
Usage
Content 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.
getContent(): string
This will just the content you put in via constructor or setContent
method.
getHrefs(): Collection
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.
getHrefsAsArray(): array
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.
getSrcUrls(): Collection
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.
getLinksFromPlaintext(): Collection
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.
getAllTheLinks(): Collection
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.
URL Helper
Some useful helper functions to handle URL related manipulating or info-gatherings.
normalize_url(string $url): string|false
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
build_url(array $parsed_url): string
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);
getMainDomainPart(string $url): string
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"
getSubdomainPart(string $url): string
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"
Sitemap Helper
Handling with web-sitemaps can be handy with some helpers at your side.
process_input_from_string(string $input_string): Collection
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
process_input_from_url(string $input_url): Collection (0.5+)
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
Tests
Simply run
composer test-ci
or
vendor/bin/phpunit
Finally
Be productive 😉