PHP code example of larva / support
1. Go to this page and download the library: Download larva/support 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/ */
larva / support example snippets
use Larva\Support\HttpClient;
use Larva\Support\StringHelper;
use Larva\Support\Number;
use Larva\Support\TimeHelper;
use Larva\Support\IDCard;
// 1. 发起一个 JSON HTTP 请求
$client = new HttpClient();
$data = $client->getJSON('https://api.example.com/users', ['page' => 1]);
// 2. 生成 UUID、随机字符串
$uuid = StringHelper::createUuid('usr_');
$rand = StringHelper::random(16);
// 3. 金额格式化(人民币)
echo Number::rmb(100.5); // ¥100.50
// 4. 友好的时间显示
echo TimeHelper::humanDateTime(time() - 3600); // 1 小时前
// 5. 校验中国大陆身份证
if (IDCard::validate('11010519491231002X')) {
echo IDCard::getSex('11010519491231002X'); // 男 / 女
echo IDCard::getBirthday('11010519491231002X'); // 1949-12-31
echo IDCard::getLocation('11010519491231002X'); // 北京市 / 朝阳区
}
use Larva\Support\HttpClient;
use Larva\Support\Exception\ConnectionException;
$client = new HttpClient([
'base_uri' => 'https://api.example.com',
'timeout' => 10,
]);
// 发起 JSON 请求
$user = $client->getJSON('/users/1');
// 提交表单
$client->post('/users', ['name' => 'Tom', 'age' => 18]);
// 上传文件
$client->upload('/avatar', '/path/to/file.png', ['field' => 'file']);
use Larva\Support\StringHelper;
StringHelper::random(32); // 随机字符串
StringHelper::createUuid(); // UUID v4
StringHelper::camel('user_name'); // userName
StringHelper::snake('UserName'); // user_name
StringHelper::studly('user_profile'); // UserProfile
StringHelper::mask('13800001234', 3, 7); // 138****1234
use Larva\Support\Number;
Number::float(1.236, 2); // 1.24
Number::priceFormat(9.8); // 9.80
Number::rmb(199.9); // ¥199.90
Number::toChinese(100.50); // 壹佰元伍角
use Larva\Support\TimeHelper;
TimeHelper::humanDateTime('2024-01-01 12:00:00'); // 相对时间描述
TimeHelper::timestampToDateTime(1704067200); // 2024-01-01 08:00:00
use Larva\Support\ArrayHelper;
use Larva\Support\Collection;
$value = ArrayHelper::getValue($array, 'user.profile.name', 'default');
$collection = new Collection([1, 2, 3, 4, 5]);
$result = $collection->map(fn ($v) => $v * 2)
->filter(fn ($v) => $v > 4)
->values();
use Larva\Support\Tree;
$rows = [
['id' => 1, 'parent_id' => 0, 'name' => 'A'],
['id' => 2, 'parent_id' => 1, 'name' => 'B'],
['id' => 3, 'parent_id' => 1, 'name' => 'C'],
];
$tree = Tree::build($rows, 'id', 'parent_id', 'children');
use Larva\Support\IDCard;
IDCard::validate('11010519491231002X'); // true / false
IDCard::getSex('11010519491231002X'); // '男' / '女'
IDCard::getBirthday('11010519491231002X'); // '1949-12-31'
IDCard::getAge('11010519491231002X'); // int
IDCard::getLocation('11010519491231002X'); // 行政区划
use Larva\Support\UnifiedSocialCreditIdentifier;
UnifiedSocialCreditIdentifier::validate('91110000600037341L'); // true