jaeger/querylist-absolute-url

QueryList 插件:将相对 URL 转换为绝对 URL。

4.0.1 2017-12-22 07:36 UTC

This package is auto-updated.

Last update: 2024-09-09 17:16:49 UTC


README

QueryList 插件:将相对 URL 转换为绝对 URL。

QueryList 插件: 将 URL 相对路径转换为绝对路径。

QueryList:https://github.com/jae-jae/QueryList

QueryList4 安装

composer require jaeger/querylist-absolute-url

API

  • absoluteUrl($baseUrl): 将页面所有 URL 转换为绝对 URL,返回 QueryList
  • absoluteUrlHelper($baseUrl,$relativeUrl): 转换辅助函数,返回 字符串

安装选项

QueryList::use(AbsoluteUrl::class,$opt1,$opt2)

  • $opt1: absoluteUrl 函数别名。
  • $opt2: absoluteUrlHelper 函数别名。

使用方法

  • 安装插件
use QL\QueryList;
use QL\Ext\AbsoluteUrl;

$ql = QueryList::getInstance();
$ql->use(AbsoluteUrl::class);
//or Custom function name
$ql->use(AbsoluteUrl::class,'absoluteUrl','absoluteUrlHelper');
  • 转换所有链接
$data = $ql->get('https://toutiao.io/')
	->absoluteUrl('https://toutiao.io/')
    ->find('a')->attrs('href');
    
print_r($data);

输出

Array
(
    [0] => https://toutiao.io/
    [1] => https://toutiao.io/explore
    [2] => https://toutiao.io/posts/hot/7
    [3] => https://toutiao.io/contribute
    [4] => https://toutiao.io/account/subscriptions
	//....
)
  • 转换辅助函数
$data = $ql->rules([
    'link' => ['a','href']
])->get('https://toutiao.io/')->query()->getData(function ($item) use($ql){
    $item['link'] = $ql->absoluteUrlHelper('https://toutiao.io/',$item['link']);
    return $item;
});

print_r($data);

输出

Array
(
    [0] => Array
        (
            [link] => https://toutiao.io/
        )
    [1] => Array
        (
            [link] => https://toutiao.io/explore
        )
    [2] => Array
        (
            [link] => https://toutiao.io/posts/hot/7
        )
    [3] => Array
        (
            [link] => https://toutiao.io/contribute
        )
    [4] => Array
        (
            [link] => https://toutiao.io/account/subscriptions
        )
    //...
)