How to pass parameters to the callback function


Parameter passing method

You can use use to pass parameters to any callback function.

Note: Only higher versions of PHP support this syntax. If an error is reported, it means that the PHP version you installed is too low.

Example

<?php
require 'QueryList/vendor/autoload.php';
use QL\QueryList;
$html =<<<STR
    <div id="demo">
        xxx
        <a href="/yyy">链接一</a>
        <a href="/zzz">链接二</a>
    </div>
STR;
$baseUrl = 'http://xxx.com';
//获取id为demo的元素下的最后一个a链接的链接和文本
//并补全相对链接
//方法一
$data = QueryList::Query($html,array(
        'link' => array('#demo a:last','href','',function($content) use($baseUrl){
            return $baseUrl.$content;
        }),
        'name' => array('#demo a:last','text') 
    ))->data;
print_r($data);
//方法二
$data = QueryList::Query($html,array(
        'link' => array('#demo a:last','href'),
        'name' => array('#demo a:last','text') 
    ))->getData(function($item) use($baseUrl){
    $item['link'] = $baseUrl.$item['link'];
    return $item;
});
print_r($data);
/**
 结果
 Array
(
    [0] => Array
        (
            [link] => http://xxx.com/zzz
            [name] => 链接二
        )
)
 */