LitUtils PHP 帮助文件.
- 此项目出生是为了"实现一些常用又不内置的PHP方法(函数)".
- 项目目录中的 demo.php 可以直观的帮助理解代码.
- 如果您有任何,请在git issure 中创建问题或者自由创建分支.
composer require lit/utils
use \Lit\Utils\LiArray;
var_dump ( LiArray::regexArray (['aa','bb','cc','ab','ac'],'/^a/') );
//array(3) {
// [0]=>
// string(2) "aa"
// [3]=>
// string(2) "ab"
// [4]=>
// string(2) "ac"
//}
var_dump (LiArray::arrayReplace('a','b',['aacd','aag','have','aa'=>['cba']]));
//array(4) {
// [0]=>
// string(4) "bbcd"
// [1]=>
// string(3) "bbg"
// [2]=>
// string(4) "hbve"
// ["aa"]=>
// array(1) {
// [0]=>
// string(3) "cbb"
// }
//}
$xml = '<?xml version="1.0" encoding="UTF-8"?> <note><to>Tove</to><from>Jani</from><heading>Reminder</heading></note>';
var_dump (LiArray::xmlToArray($xml));
//array(3) {
// ["to"]=>
// string(4) "Tove"
// ["from"]=>
// string(4) "Jani"
// ["heading"]=>
// string(8) "Reminder"
//}
var_dump(LiArray::getValues(["a" => 1, "b" => 2, "c" => 3, "d" => 4], ["a", "c"]));
//array(2) {
// ["a"]=>
// int(1)
// ["c"]=>
// int(3)
//}
$a = ["key" => "key1", "val" => "val2", "good" => "good3",1=>"11",0]; //待排序的数组
$b = ["good", "key",1,"val"]; //想要的顺序
var_dump ( LiArray::sortByArray($a, $b) );
//array(5) {
// ["good"]=>
// string(5) "good3"
// ["key"]=>
// string(4) "key1"
// [1]=>
// string(2) "11"
// ["val"]=>
// string(4) "val2"
// [2]=>
// int(0)
//}
var_dump(LiArray::keyAlias(["_id" => 1, "_name" => "测试"], ["_id" => "id", "_name" => "name"]));
//array(2) {
// ["id"]=>
// int(1)
// ["name"]=>
// string(6) "测试"
//}
$array = [
["name" => "小美", "group_id" => "2"], ["name" => "小帅", "group_id" => "1"],
["name" => "阿呆", "group_id" => "2"], ["name" => "阿花", "group_id" => "1"],
["name" => "烧饼", "group_name" => "3"],
];
var_dump(LiArray::groupByKey($array, "group_id", "other_group"));
use \Lit\Utils\LiString;
var_dump( LiString::str2num('九千五百一十三兆九千零三亿一千零二十七万零二佰五十') );
//int(9513900310270250)
var_dump ( LiString::randStr(8,true,true,true,true) );
//string(8) "to6HG!w6"
var_dump ( LiString::subStrTo('i can say my abc !',' my') );
//string(9) "i can say"
var_dump ( LiString::strEncode('可逆运算') );
//string(16) "Y6D+65K6L5i+L6TP"
var_dump ( LiString::strDecode("Y6D+65K6L5i+L6TP") );
//string(12) "可逆运算"
var_dump(LiString::strLimit('不a管b什么样的字符都可以了', 7));
//string(11) "不a管b..."
var_dump(LiString::replaceStringVariable('我叫{$name}', array('name' => 'litool')));
//string(12) "我叫litool"
var_dump(LiString::toCamelCase("hello_my_name_is8a_h5_array"));
//string(22) "helloMyNameIs8aH5Array"
var_dump(LiString::toUnderScoreCase("HelloMyNameIs8aH5Array"));
//string(26) "hello_my_name_is8a_h5array"
var_dump(LiString::array2sql(["name" => "test", "id" => 12],"table1", "database"));
//string(71) "insert into `database`.`table1` ( `name`, `id` ) values ( "test", "12" )"
var_dump(LiString::getIdsByStr("123\n9900,1231,4333分割889"));
//array(5) {
// [0]=>
// string(3) "123"
// [1]=>
// string(4) "9900"
// [2]=>
// string(4) "1231"
// [3]=>
// string(4) "4333"
// [4]=>
// string(3) "889"
//}
var_dump(LiString::hasMessyCodes("是否含有乱码"));
//bool(false)
var_dump(LiString::gb2u("GB18030 Code"));
//string(10) "UTF-8 Code"
var_dump(LiString::u2gb("UTF-8 Code"));
//string(12) "GB18030 Code"
use \Lit\Utils\LiDate;
var_dump ( LiDate::microTime() );
//string(17) "1623396847.630771"
var_dump ( LiDate::milliTime() );
//string(14) "1623396847.631"
var_dump ( LiDate::dateFormat(time()-mt_rand(1,9999)) );
//string(10) "1分钟前"
var_dump ( LiDate::nextMonth('2015-2-28') );
//string(7) "2015-03"
var_dump ( LiDate::lastMonth('2015-01-01') );
//string(7) "2014-12"
var_dump ( LiDate::todayRemainTime() );
//int(58922)
use \Lit\Utils\LiMath;
var_dump ( LiMath::base10to62(40000) );
//string(3) "apa"
var_dump ( LiMath::base62to10('ACG97') );
//int(541166573)
var_dump ( LiMath::between(6,1,6) );
//bool(true)
use \Lit\Utils\LiSundry;
var_dump ( LiSundry::getRemoteAddr() );
//string(7) "192.168.1.123"
LiSundry::sendHttpStatus(404) ;
$wd = array (
array ( 'w' => 60, 'v' => '我是60%概率') ,
array ( 'w' => 35, 'v' => '我是35%概率') ,
array ( 'w' => 5 , 'v' => '我是5%概率') ,
);
var_dump ( LiSundry::getWeight( $wd ) );
//string(15) "我是35%概率"
var_dump ( LiSundry::isLocalIp('10.25.11.58') );
//bool(true)
var_dump ( LiSundry::isIdNumber('130602199001011111',1) );
//bool(true)
注意: 此系统只适用于简单身份认证,不适合高并发,大数据量,高安全性的用户系统
$easyAuth = \Lit\Utils\LiEasyAuth::getInstance("/opt/data/cache/easyAuth");
var_dump ( $easyAuth->addUser("lit","1233333") );
var_dump ( $easyAuth->userExists("lit") );
var_dump ( $easyAuth->userExists("abb") );
var_dump ( $easyAuth->getUserInfo("lit"));
var_dump ( $easyAuth->getUserInfo("abb"));
var_dump ( $easyAuth->checkLogin("lit","1233333"));
var_dump ( $easyAuth->checkLogin("lit","12332333"));
var_dump ($easyAuth->hasUser());
$easyAuth->delUser("lit");
$easyAuth->delUser("abb");
var_dump ( $easyAuth->getDataBaseDir() );
#服务端验证访问
$sign = new \Lit\Utils\LiSignature();
//自定义参与运算的 accessKeyId accessKeySecret
$accessData["accessKeyId"] = "accessKeySecret";
$accessData["accessKeyId2"] = "accessKeySecret2";
$accessData["accessKeyId3"] = "accessKeySecret4";
//获取其他参数
$urlPath = "/api/aaa";
$get = $_GET;
$post = $_POST;
$accessKeySecret = $accessData[$get["AccessKeyId"]];
var_dump ( $sign->checkSignature($urlPath, $accessKeySecret, $get, $post , 'callBackFunction' ));
var_dump ( $sign->getSignatureString());
var_dump ( $sign->getErrorString() );
function callBackFunction ( $signatureNonce ) {
//此函数需自行实现, 用于防止网络重放攻击.
//$signatureNonce 为不重复字符串, 使用过返回true, 没有使用过返回false
//可把此值保存至redis或者memcache等数据库中并设置有效或定时清理
return false;
}
#客户端访问构建
//GET参数
$sign->addGetParam("AccessKeyId","accessKeyId");
$sign->addGetParam("Version","version");
$sign->addGetParam("SignatureNonce","signaturenonce");
$sign->addGetParam("Timestamp",time() );
$sign->addGetParam("OtherParam","aa");
$sign->addGetParam("OtherParam2","bb");
//POST参数(可选)
$sign->addPostParam("aa","bb");
$sign->addPostParam("bb","bb");
$sign->addPostParam("cc","cc");
#获取待请求url
var_dump ( $url= $sign->getQueryUrl("http://192.168.11.187:9000","/testServer.php","accessKeySecret" ) );
#获取待签名字符串 调试用
var_dump ($sign->getSignatureString());
#获取错误代码
var_dump ( $sign->getErrorCode() );
#获取错误提示
var_dump ( $sign->getErrorString() );
#请求方式示例
var_dump ( (new \Lit\Utils\LiHttp())->get($url)->send() );
try {
$file = \Lit\Utils\LiVersionFile::put("./tmp", date("YmdHi") . '.txt', './version/', uniqid());
var_dump($file);
} catch (Exception $e) {
var_dump($e->getCode(), $e->getMessage());
}
try {
$file = \Lit\Utils\LiVersionFile::deleteVersionFile("./version", '202012181009.txt.ver.2020121810093031802537');
var_dump($file);
} catch (Exception $e) {
var_dump($e->getCode(), $e->getMessage());
}
try {
$list = \Lit\Utils\LiVersionFile::versionFileList('./version', '20201218093840.txt');
foreach ($list as $file) {
echo $file, "\n";
}
} catch (Exception $e) {
var_dump($e->getCode(), $e->getMessage());
}
$http = new \Lit\Utils\LiHttp();
$response = $http->setParam(["a" => 1])->get("http://www.google.com?b=2")->send();
var_dump($response);
$response = $http->setParam(["a" => 1])->post("http://www.google.com?b=2")->send();
var_dump($response);
$response = $http->postJson("http://www.google.com", "{}")->setHeader(["aa:11"])->send();
var_dump($response);
$response = $http->setParam(["a" => 1])->postFile(["file" => "/Users/Desktop/comic.zip", "file2" => "/Users/Desktop/2.jpg"])->post("http://google.com")->send();
var_dump($response);
$response = $http->get("https://www.google.com/aaa.jpg")->setSavePath("./aaa.jpg")->download(false);
var_dump($response);
class TestMapper extends \Lit\Utils\LiMapper
{
protected $a = 1;
protected $b = 2;
protected $c = 3;
protected $d = 3;
}
$newMapper = new TestMapper();
$newMapper->a = 30;
$newMapper->d = 50;
var_dump($newMapper->getUpdate()); //获取修改数据
//array(2) {
// ["a"]=>
// int(30)
// ["d"]=>
// int(50)
//}
var_dump($newMapper->getInsert()); //获取写入数据
//array(4) {
// ["a"]=>
// int(30)
// ["b"]=>
// int(2)
// ["c"]=>
// int(3)
// ["d"]=>
// int(50)
//}
$newMapper = new TestMapper(["a" => 10, "b" => "15"]);
var_dump($newMapper->getUpdate()); //获取修改数据
//array(2) {
// ["a"]=>
// int(10)
// ["b"]=>
// string(2) "15"
//}
var_dump($newMapper->getInsert()); //获取写入数据
//array(4) {
// ["a"]=>
// int(10)
// ["b"]=>
// string(2) "15"
// ["c"]=>
// int(3)
// ["d"]=>
// int(3)
//}
use \Lit\Utils\LiSelector;
$liSelector = new TestSelector();
$liSelector->book_id->notEqual(2);
$liSelector->book_name->equal("str");
//$liSelector->status->lessThan(20);
//$liSelector->status->greaterEqual(0);
//$liSelector->age->between(10, 20);
//$liSelector->age->in(["1", "2", "3"]);
var_dump($liSelector->getCondition()); //获取构建条件
//array(2) {
// [0]=>
// array(3) {
// [0]=>
// string(7) "book_id"
// [1]=>
// string(2) "!="
// [2]=>
// int(2)
// }
// [1]=>
// array(3) {
// [0]=>
// string(9) "book_name"
// [1]=>
// string(1) "="
// [2]=>
// string(3) "str"
// }
//}
var_dump($liSelector->getSql()); //获取构建条件生成的SQL
//array(2) {
// [0]=>
// string(16) "`book_id` != "2""
// [1]=>
// string(19) "`book_name` = "str""
//}
try {
$mySelector = new TestSelector([
["book_id", ">", 1],
["book_name", "=", "哈哈"]
]);
var_dump($mySelector->getCondition());
// array(2) {
// [0]=>
// array(3) {
// [0]=>
// string(7) "book_id"
// [1]=>
// string(1) ">"
// [2]=>
// int(1)
// }
// [1]=>
// array(3) {
// [0]=>
// string(9) "book_name"
// [1]=>
// string(1) "="
// [2]=>
// string(6) "哈哈"
// }
// }
var_dump($mySelector->getSql());
// array(2) {
// [0]=>
// string(15) "`book_id` > "1""
// [1]=>
// string(22) "`book_name` = "哈哈""
// }
} catch (\Exception $exception) {
var_dump($exception);
}
用于全局数据传输, 避免污染 $GLOBALS
use \Lit\Utils\LiTransit;
var_dump(LiTransit::set("a", 2));
var_dump(LiTransit::setNX("a", 4));
var_dump(LiTransit::get("a"));
var_dump(LiTransit::exists("c"));
var_dump(LiTransit::rm("b"));
var_dump(LiTransit::getAll());