PHP
推荐链接
ThinkPHP
TP5在前端模板对数据的处理方式
1.number_format
{$number|number_format=2} 千分位,保留两位小数
2.round
{$number|round=2} 四舍五入保留两位小数
value="{:session('user.UserName');}"
value="{:date('Y-m-d H:i:s',time());}"
value="{$data.createTime|date='Y-m-d H:i:s'}"
1、截取字符串正确的写法
{$a['a']['b']|substr=0,2} //显示前面,两个字符
{$a['a']['b']|substr=0,-2}//删除后面,两个字符
{$a['a']['b']|substr=2,-2}//删除前后,两个字符
{$a['a']['b']|substr=-4,2}//显示后4到前进2字符
{$a['a']['b']|substr=-4,-2}//显示后4到后2字符
第一个属性是定位,第二个是移动。
正数是前进。负数是才尾巴开始往前数。
2、截取汉字的正确的正确写法:
{$ma.title|mb_substr=0,5,'utf-8'}
第一个属性是定位,第二个是移动。第三个是编码格式
TP5导入Excel(读取Excel里面的内容)
public function importExcel($file = null)
{
include('./vendor/PHPExcel/PHPExcel.php');
//接收上传文件
if (!$file) {
return return_array_result(303, '缺少file');
}
$info = $file->move('/public/Uploads/');
$getSaveName = str_replace("\\", "/", $info->getSaveName());
// 读取刚刚上传文件保存的路径
$fileUrl = '/public/Uploads/' . $getSaveName;
$ext = pathinfo($fileUrl);
//设置excel格式
$type = strtolower($ext["extension"]);
$excel_type = $type==='csv' ? $type : 'Excel2007';
$reader = \PHPExcel_IOFactory::createReader($excel_type);
$excel = $reader->load($fileUrl);
$excel_array = $excel->getSheet(0)->toArray();
// 在控制台打印出来
return (json_encode(array('status' => 1,'tete' => $ext,'test' => $excel_array)));
}
TP5 定时任务
namespace app\index\command;
use think\console\Command;
class Task extends Command{
protected function configure(){
/**
这里的setName必须和php文件名一致,setDescription里面的就是备注
如果类名是MyTask 则 $this->setName('mytask')->setDescription('My custom task');*/
$this->setName(task')->setDescription("每天统计数据");
//可以直接链式链接到$this后面
// 添加位置参数
$this->addArgument('param1', Argument::REQUIRED, '必选参数 Parameter 1 description');
// 添加选项参数
$this->addOption('param2', 'p', Option::VALUE_REQUIRED, '可选参数 Parameter 2 description');
}
protected function execute(Input $input, Output $output)
{
/**
Input 对象表示命令行输入流,可以用于获取用户在命令行中输入的参数、选项和其他数据。通过 Input 对象,您可以获取命令行中传递的参数值、选项值以及其他输入流相关的信息。
Output 对象表示命令行的输出流,用于向命令行输出信息和结果。通过 Output 对象,您可以向命令行输出文本、表格、进度条等内容。
*/
$output->writeln('定时命令开始执行...');
//这里写业务逻辑(如果想要获得用户输入,则必须要设置configure函数里面的位置参数或者选项参数)
$param1 = $input->getArgument('param1');
$param2 = $input->getOption('param2');
$output->writeln('必选参数1的值为:' . $param1);
$output->writeln('可选参数2的值为:' . $param2);
$output->writeln('定时命令执行已经结束...');
}
}
//command.php 在这个文件中添加这一行
return [
'app\index\command\Task' //这里把上面的任务php文件填上
];
//在项目根目录执行下面这个命令可以看到我们自定义的命令
php think
//如果因为setDescription("中文"); 里面是中文备注,导致乱码。
//可以在属性里面的当前代码页切换字符编码,如果无法切换则使用命令行进行切换
//查看字符编码
chcp
// 切换到 65001(UTF-8)
chcp 65001
//启动任务
php think task
//带参数版
php think task param1value1 --param2=param2value2
//如果需要写bat文件,则可以使用下面的启动方式
//注意项目的根目录,先进入task文件所在TP框架的根目录
cd:xxx
//D:\wamp64\bin\php\php5.6.25\php.exe think task
D:\php的执行路径\php.exe think task// 用php.exe打开think Task任务
// 如果执行上面的有问题,可以试一试下面这个
php -c D:\wamp64\bin\php\php5.6.25\phpForApache.ini
D:\php的执行路径\任务类.php
TP5执行存储过程
MySql
//Db::execute()方法返回的是执行SQL语句后的受影响行数。
//Db::query()返回的是存储过程的返回值
//第一种
$result = Db::execute('EXEC 存储过程名称 (?, ?)', [参数一,参数二]);
//第二种
$result = Db::query("call 存储过程名称 ('变量1','变量2','变量3')");
$row = $result->fetch();
$status = $row['status']; // 假设存储过程返回一个名为status的字段
Sqlserver
//执行存储过程
//(占位符 方式) 因为有的存储过程需要传入的参数 类型是 int ,如果直接执行存储过程会导致框架自动加上引号
// 第一种
$result = Db::execute('EXEC 存储过程名称 ?, ?', [参数一,参数二]);
//第二种
$sql = "SELECT dbo.Z_F_GetOtherCurrAmount(:test1, :test2, :test3) AS test";
$params = [
"test1" => $val["test1"],
"test2" => $tableData['test2'],
"test3" => $val["test3"]
];
$result2 = Db::query($sql, $params);
TP5 日志相关
日志的相关类和方法
在ThinkPHP5中单纯的 Log::getLog(); 这里面获取最后一条日志记录,但是有可能里面的信息无法满足业务的具体要求这里就需要自己手动写入,例如:无法获得具体的时间戳,这里只能自己手动写入具体的时间戳
配置密码策略类
配置密码策略,复杂度需满足大小写字母、数字、特殊符号(4选3),长度最少8位
class PasswordPolicy {
private $requiredLength;
private $requiredCharacterSets;
public function __construct($length = 8, $characterSets = 3) {
$this->requiredLength = $length;
$this->requiredCharacterSets = $characterSets;
}
public function validatePassword($password) {
$characterSetsCount = 0;
// 检查是否包含小写字母
if (preg_match('/[a-z]/', $password)) {
$characterSetsCount++;
}
// 检查是否包含大写字母
if (preg_match('/[A-Z]/', $password)) {
$characterSetsCount++;
}
// 检查是否包含数字
if (preg_match('/\d/', $password)) {
$characterSetsCount++;
}
// 检查是否包含特殊符号
if (preg_match('/[@$!%*?&]/', $password)) {
$characterSetsCount++;
}
// 判断是否满足密码策略
return strlen($password) >= $this->requiredLength && $characterSetsCount >= $this->requiredCharacterSets;
}
}
// 测试
$passwordPolicy = new PasswordPolicy(8, 3);
$password1 = "ABC123!"; // 符合规则
var_dump($passwordPolicy->validatePassword($password1)); // 输出 bool(true)
报错信息:SplFileInfo::getSize(): stat failed for
先执行 move() 移动文件后,导致文件移除之后又执行了:$size = $file->getSize() 方法