PHP经典实例(php cookbook)读书笔记 4.2 (0x01-0x03)
从17年4月份开始看这本书,之前1年跟着做过几个小的项目但是都是作为一个搬运工,还好是一个学生有的是时间看看每段代码的思路,但有时间看看框架架构,总是有局限,现在开始看看相关书籍,一是减少项目上查goolgle和baidu的次数,二是给自己机会进行二次提升。SO,这是基础书籍学习后的第一本书。
目录
0x01 字符串
4个常用字符串子串处理函数
- int strpos(string $haystack , mixed $needle [, int $offset = 0 ])取子串位置
- string substr ( string $string , int $start [, int $length ] )由位置取子串
- mixed substr_replace ( mixed $string , string $replacement , int $start [, int $length ] )子串替换
- int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )查询子串出现次数
分割翻转
就是把段落依据空格分段存入数组,然后翻转数组再组合
$reverser_s=implode('',array_reserse(preg_split('/\W/',$s)));
随机字符串生成
用mt_rand生成的随机整数击中给定的特征串中的字符
$need_string_length;
$characters='0123456789zxcasdfqwer';
for($i=0;$i<$need_string_lendth;$1++)
$need_string.=$characters[mt_rand(0,$strlen($characters)-1)];
- 控制大小写
首字母大写
-ucfirst();
所有单词首字母大写
-ucwords();
所有字母大写
-strtoupper();
所有字母小写;
-strtolower();
固定宽度字段解析
1. 先用explode 或者preg_split 按行分割
2. 使用substr取出固定字段
3. trim去前后空格
$fields=arry('title'=>10,
'author'=> 10,
'publication_year'=>'4');
$data=<<END
English lilei 2014
Chinese wangming 2016
History luoyu 2011
END;
$r=fixed_width_substr($fields,$data);
function fixed_width_substr($fields,$data){
$data_rows=explode('\n',$data)
$r =array//返回集
foreach(data_rows[$i] as $row){
$row_flag=0;
foreach($fields as $field_name => $field_length){ $r[$i[$fileds_name]=trim(substr($data[$i],$row_flag,$field_length));
$row_flag += $fidld_length;
}
}
return $r;
}
文本定长换行
wordwrap(string,width,break,cut)
参数 描述
string 必需。规定要进行折行的字符串。
width 可选。规定最大行宽度。默认是 75。
break 可选。规定作为分隔符使用的字符(字串断开字符)。默认是 “\n”。
cut 可选。规定是否对大于指定宽度的单词进行折行。
pack 以及unpack
网上资料很全面
主要应用是存储和解析二进制流
正则的分割、匹配、替换
分割
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int \$flags = 0 ]]
匹配
int preg_match ( string
pattern,string
subject [, array &
matches[,int
flags = 0 [, int $offset = 0 ]]] )
替换
在$subject为数组时,不返回未匹配的项
mixed preg_filter ( mixed
pattern,mixed
replacement , mixed
subject[,int
limit = -1 [, int &$count ]] )
在$subject为数组时,返回所有项
mixed preg_replace ( mixed
pattern,mixed
replacement , mixed
subject[,int
limit = -1 [, int &$count ]] )
csv文件处理
可下载的csv
用fopen()打开文件句柄
$output=fopen(‘php://output’,’w’);
用header()改变程序输出内容
header(‘Content-Type: application/csv’);
header(‘Content-Disposition: attachement; filename=”sales.csv”’);
用fputcsv()逐行写数据
fputcsv(
output,
data);
关闭文件句柄
fclose($output)
0x02 数字
监测变量是否数字
用is_numeric即可
但是使用的时候要去除实在的千位分隔符
除此之外还有 is_float(),is_double(),is_real(),is_interger(),is_double()
浮点数舍进
向大的取整ceil($num);
-2.*->2
2.*->3向小的取整floor($num);
-2.*->3
2.*->2四舍五入round( num, precision, mode);默认五入的方向是远离0可以设置 mode改变舍入规则
-2.5->3
2.5->3
$precision是精确位数
如果要设置常规的四舍五入
(正数五入远离0,负数五入接近0)
可以这样
founction define_round($num,$precision){
return $num>0?round($num,$precision):-round(abs($num),$precision)
}
权重随机数
将权重拼接成一个靶子范围,然后使用mt_rand生成的随机数击打靶子
function rand_weighted($numbers){
$total=0;//靶场
foreach ($$numbers as $$number => $weight) {
$total += $weight;
$distribution[$number] = $total;
}
$rand =mt_rand(0, $total-1);
foreach ($distribution as $number => $weight) {
if ($rand < $weight) {
return $number;
}
}
}
数学计算
log(),log10(),exp(),pow(); 当结果超过约1.8E308时会返回INF
各种三级函数 cos(),sin()等,参数默认为弧度数,
当参数不是弧度数时使用deg2rad()转化。
千分位添加与删除
string number_format(number,decimals,decimalpoint,separator)
number 必需。要格式化的数字。如果未设置其他参数,则数字会被格式化为不带小数点且以逗号(,)作为千位分隔符。
decimals 可选。规定多少个小数。如果设置了该参数,则使用点号(.)作为小数点来格式化数字。
decimalpoint 可选。规定用作小数点的字符串。
separator 可选。规定用作千位分隔符的字符串。仅使用该参数的第一个字符。比如 “xxx” 仅输出 “x”。
1。此函数返回的是一个字符串
2。它第二个参数设置了精确度时,规则和round一样,0.5向远离0进位
删除千分位
str_replace(',', '', number_format(12345678.123,2);
大数的处理
使用BCMath 和 GMP库;
进制转换
例子
decbin 10->2
bindec 2->10
bin2hex 2->16
hex2bin 16->2
oct2hex 8->16
oct2bin 8->2
以此推类
另外
base_convert() 函数在任意进制之间转换数字。
语法
base_convert(number,frombase,tobase);
参数 描述
number 必需。规定要转换的数。
frombase 必需。规定数字原来的进制。介于 2 和 36 之间(包括 2 和 36)。
tobase 必需。规定要转换的进制。介于 2 和 36 之间(包括 2 和 36)。
注意:高于十进制的数字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。
0x03 日期和时间
日期和时间戳的获取和转化
获得当前时间date(‘r’);
Sun, 02 Apr 2017 09:11:02 +0000
获得当前时间戳time();
1491124262
查找时间部分
var_dump(getdate());
array (size=11)
‘seconds’ => int 36//秒
‘minutes’ => int 12//分钟
‘hours’ => int 9
‘mday’ => int 2//一月中那一天
‘wday’ => int 0//星期几 0为星期天
‘mon’ => int 4//月
‘year’ => int 2017//年
‘yday’ => int 91//一年中哪一天
‘weekday’ => string ‘Sunday’ (length=6)
‘month’ => string ‘April’ (length=5)
0 => int 1491124356
var_dump(localtime()) ;
array (size=9)
0 => int 21//秒
1 => int 13//分
2 => int 9//小时
3 => int 2//月中哪一天,第一天为1
4 => int 3//几月, 一月为0
5 => int 117//从 1900 年以来那一年
6 => int 0//星期几
7 => int 91//一年中哪一天
8 => int 0?//是否实行日光节省时间
另外 使用的时候需要用date_default_timezone_set(‘Asia/Shanghai’);来设置为中国时区
时间戳和日期和互换
日期转为时间戳
gmmktime(hour,minute,second,month,day,year,is_dst);
时间戳转为日期
date(“Y-m-d”,$t)//常用格式Y m d h i s w(星期天为0)
或者使用getdate($t)和localtime($t)
两个日期之差
有两种方式
一个是转为时间戳,然后相减,再使用getdate()和localtime()转回可操作日期
一个是使用Datatime对象的diff方法
另外开发是时候注意结束日期的时间应该是23:59:59 而不是 0:0:0
解析字符串日期时间以及验证日期合法
checkdate(3,10,1993);
使用strtotime()可以解析一切可读时间转换为时间戳
例如
echo date(' Y-m-d h:i:s',strtotime(' may 12 2000'));
2000-05-12 12:00:00
echo date('Y',strtotime(' last year'));
2016
高精度时间
microtime(get_as_float);
参数 描述
get_as_float 可选。当设置为 TRUE 时,规定函数应该返回一个浮点数,否则返回一个字符串。默认为 FALSE。
技术细节
返回值: 默认返回字符串 “microsec sec” ,其中 sec 为自 Unix 纪元(0:00:00 January 1, 1970 GMT)起的秒数,microsec 为微秒部分。如果 get_as_float 参数设置为 TRUE,则返回一个浮点数,表示自 Unix 纪元起精确到微秒的以秒为单位的当前时间。
PHP 版本: 4+
更新日志: PHP 5.0.0:新增 get_as_float 参数。