PHP code example of xuchen / parser
1. Go to this page and download the library: Download xuchen/parser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xuchen / parser example snippets php
namespace App;
use Xuchen\Parser\Parser;
use Xuchen\Parser\Helper;
class MyParser extends Parser
{
// Parser中提供的一些辅助方法
use Helper;
// 字段整理规则,为每一个字段设置一个回调函数
protected function parseRules()
{
// 未设置回调函数的字段则直接返回原值或null
return [
'gender' => function($row) {
// getValue()是父类中提供的一个获取hash-table中的值的方法
$gender = $this->getValue($row, 'gender', 'm');
return $gender == 'm'? '男' : '女';
},
'mobile' => function($row) {
// getValueRecursively()是父类中提供的一个以递归方式获取多层hash-table中的值的方法
$mobile = $this->getValueRecursively($row, 'profile.mobile', '');
if ($mobile) {
// hideMobile()是Helper中提供的一个隐藏手机号的方法
return $this->helper->hideMobile($mobile);
} else {
return $mobile;
}
}
];
}
// 单行数据的整理规则
protected function parseSingleRow($row)
{
$newRow = [];
$newRow['id'] = $this->getValue($row, 'id', 0);
$newRow['name'] = $this->getValue($row, 'name', '');
$newRow['gender'] = $this->getValue($row, 'gender', 'm') == 'm'? '男' : '女';
$mobile = $this->getValueRecursively($row, 'profile.mobile', '');
$newRow['mobile'] = $mobile? $this->hideMobile($mobile) : '';
return $newRow;
}
}