spresnac/laravel-url-helper

帮助您解决与URL相关的问题

0.9.1 2023-04-15 19:03 UTC

This package is auto-updated.

Last update: 2024-09-15 22:03:12 UTC


README

Software License PHP from Packagist codecov CI Status

内容

安装

首先,所以需要安装这个包

composer require spresnac/laravel-url-helper

使用

内容助手

每次您需要从某种文本或HTML中获取URL时,这个类都是您的得力助手。

  1. 创建类的实例
  2. 使用构造函数或 setContent 方法将内容作为字符串放入。
  3. 使用以下方法之一获取您的URL。

getContent(): string

这将是您通过构造函数或 setContent 方法放入的内容。

getHrefs(): Collection

您将检索一个只包含 href 内部URL的 Collection。

$content_helper = new ContentHelper('insert HTML here');
$result = $content_helper->getHrefs();
// $result is an Collection with all the hrefs in it.

getHrefsAsArray(): array

您将检索一个只包含 href 内部URL的数组。

$content_helper = new ContentHelper('insert HTML here');
$result = $content_helper->getHrefsAsArray();
// $result is an Collection with all the hrefs in it.

getSrcUrls(): Collection

您将检索一个来自 src 源的 URL 集合。

$content_helper = new ContentHelper('insert HTML here');
$result = $content_helper->getSrcUrls();
// $result is an Collection with all the src-urls in it.

getLinksFromPlaintext(): Collection

此方法将尝试在纯文本字符串中查找所有URL。

$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

这将执行 getHrefsgetSrcUrlsgetLinksFromPlaintext,并返回所有唯一URL的集合。

$content_helper = new ContentHelper('insert some html or plaintext here');
$result = $content_helper->getAllTheLinks();
// $result is an Collection with all the urls.

URL助手

一些有用的辅助函数,用于处理URL相关的操作或信息收集。

normalize_url(string $url): string|false

使用 .. 正规化URL,并保留其他所有URL“原样”。

$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

在发生错误时返回 false

build_url(array $parsed_url): string

这是 parse_url 的缺失相反操作,用于从其部分重新构建URL。

$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

这将返回URL的“主要域名”部分。主要域名类似于 SLD + TLD 域名部分。

$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

这将返回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"

站点地图助手

借助一些辅助函数,可以方便地处理网站地图。

process_input_from_string(string $input_string): Collection

将返回网站地图中的所有URL作为 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+)

将返回网站地图URL中的URL作为 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

测试

简单地运行

composer test-ci

vendor/bin/phpunit 

最后

提高生产力 😉