php 杂记【一些开发的技巧、坑、注意事项等】

  1. php中如果要让正则匹配出一个\字符,则需要在php中的pattern中添加四个\字符,经过php解释器解释成两个\字符,再有正则解释器解释成一个字符
    对于有多个\出现的地方,将两个\看作一个就行
str_replace('\\', '\\\\\\\\', $pV);
//匹配一个$符号,需要前置3个反斜杠
preg_match_all("/[[:blank:]]+\\\$/", $str, $match);
  1. 让Json更懂中文(JSON_UNESCAPED_UNICODE)
  1. foreach循环后留下悬挂指针
$arr = [1,2,3];
foreach ($arr as &$v) {

}
print_r($arr);

foreach ($arr as $v) {

}
print_r($arr);
//结果
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 2
)

原因:第一次循环遍历后,$v指向数组的最后一个元素$arr[2],第二次遍历时,$arr[0]的值赋给$v, 此时$arr[2]的值被改变为1,接着$arr[1]的值赋给$v, $arr[2]的值变为2

  1. strtotime的坑
<?php
var_dump(date("Y-m-d",strtotime("-1 month",strtotime("2017-03-31"))));
//输出2017-03-03
var_dump(date("Y-m-d",strtotime("+1 month",strtotime("2017-08-31"))));
//输出2017-10-01
var_dump(date("Y-m-d",strtotime("next month",strtotime("2017-01-31"))));
//输出2017-03-03
var_dump(date("Y-m-d",strtotime("last month",strtotime("2017-03-31"))));
//输出2017-03-03

其实这个是由于strtotime的算法问题,比如2017-03-31 减一个月 变成 2017-02-31,但2月哪有31号 于是时间往后变成 2017-03-03,解决方案: 在短字串前加下first day of 或者 last day of

var_dump(date("Y-m-d",strtotime("first day of next month",strtotime("2017-01-31"))));
//2017-02-01
  1. auto_prepend_file 与 auto_append_file 脚本执行前或者执行后,自动引入相关文件。
    如果使用的是apache,则可以修改.htaccess中添加:
    php_value auto_prepend_file "D:\phphome\test\junkfile"
  1. 在PHP中,自定义的函数名,类名,以及内置的函数,关键字是不区分大小写的【卧槽,开发了3年了,才知道这事!!!】
  1. 表单中MAX_FILE_SIZE隐藏域要生效的话,需要放在file输入控件的前面。否则不会有任何效果!!!!

8 .php中生成uuidv4

	function gen_uuid() {
		$uuid = array(
			'time_low'  => 0,
			'time_mid'  => 0,
			'time_hi'  => 0,
			'clock_seq_hi' => 0,
			'clock_seq_low' => 0,
			'node'   => array()
		);

		$uuid['time_low'] = mt_rand(0, 0xffff) + (mt_rand(0, 0xffff) << 16);
		$uuid['time_mid'] = mt_rand(0, 0xffff);
		$uuid['time_hi'] = (4 << 12) | (mt_rand(0, 0x1000));
		$uuid['clock_seq_hi'] = (1 << 7) | (mt_rand(0, 128));
		$uuid['clock_seq_low'] = mt_rand(0, 255);

		for ($i = 0; $i < 6; $i++) {
			$uuid['node'][$i] = mt_rand(0, 255);
		}

		$uuid = sprintf('%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
			$uuid['time_low'],
			$uuid['time_mid'],
			$uuid['time_hi'],
			$uuid['clock_seq_hi'],
			$uuid['clock_seq_low'],
			$uuid['node'][0],
			$uuid['node'][1],
			$uuid['node'][2],
			$uuid['node'][3],
			$uuid['node'][4],
			$uuid['node'][5]
		);

		return $uuid;
	}
  1. windows中配置CURLOPT_CAPATH是不起作用的,CURLOPT_CAPATH是配置证书所在目录
  1. 使用php内置服务器
    php -S 0.0.0.0:4000 -c app/config/php.ini
  1. 除法运算符总是返回浮点数。只有在下列情况例外:两个操作数都是整数(或字符串转换成的整数)并且正好能整除,这时它返回一个整数。
  1. php正则表达式修饰符U, 表示逆转贪婪模式,可以使?的作用相反。
  1. http_build_query对于值为null的元素,不会出现在结果字串中
$a = ['a'=>1,'b'=>2,'c'=>null];
var_dump(http_build_query($a));
// string(7) "a=1&b=2"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值