1、截取字符串,要求最多占据N个汉字的位置
public function GetSubStrShown($str, $showLength, $strEncoding = 'utf8') {
$result = '';
$len = 0;
for($i = 0, $count = mb_strlen ( $str, $strEncoding ); $i < $count; $i ++) {
$tmp = mb_substr ( $str, $i, 1, $strEncoding );
$tmpLen = strlen ( mb_convert_encoding ( $tmp, "gbk", $strEncoding ) );
if ($len + $tmpLen > $showLength)
return $result;
$len += $tmpLen;
$result .= $tmp;
}
return $result;
}
2、根据提供的日期获取当前周的全部日期信息
function get_week($time = '', $format='Y-m-d'){
$time = $time != '' ? $time : time();
$week = date('w', $time);
$date = [];
for ($i=1; $i<=7; $i++){
$date[$i] = date($format ,strtotime( '+' . $i-$week-1 .' days', $time));
$date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $time));
}
return $date;
}
3、中文字符串中间部分字符替换操作
一、步骤:1、计算长度
2、根据长度截取第一部分字符串
3、截取中间部分字符串
4、截取最后一部分字符串或者去掉前两部分字符串
5、替换中间部分字符串为星号
6、连接字符串
function ceshi($username){
$length = mb_strlen($username,'utf-8');
$firstStr1 = mb_substr($username,0,ceil($length/4),'utf-8');
$firstStr = mb_substr($username,ceil($length/4),floor($length/2),'utf-8');
$firstStr2 = mb_substr($username,ceil($length/4) + floor($length/2), floor(($length+1)/2 - 1),'utf-8');
return $firstStr1 . str_repeat("*",mb_strlen($firstStr,'utf-8')) . $firstStr2;
}
function str_replace_once($username,$find,$replace)
{
$pos = mb_strpos($username,$find);
return substr_replace($username, $replace, $pos, strlen($find));
}
1、mb_strlen()
功能:返回字符串的长度。优势: 可以通过设置字符编码从而返回对应的字符数,很好的处理了中文字符串的长度问题。
语法:strlen(string $str,string $encoding)
str:字符串内容
encoding:编码格式 比如:gbk/utf8/gb2312/不填 不同的编码格式对于中文来讲长度不一样,默认为utf8
代码实例:
$str='33三三three';
echo strlen($str).'<br>';
echo mb_strlen($str,'utf8').'<br>';
echo mb_strlen($str,'gbk').'<br>';
echo mb_strlen($str,'gb2312').'<br>';
2、mb_substr()
功能:返回字符串的一部分,优势:适用于中文字符的分割
语法:mb_substr ( string $str , int $start , int $length ,string $encoding)
str:字符串内容
start:开始位置 正数 - 在字符串的指定位置开始;负数 - 在从字符串结尾的指定位置开始;0 - 在字符串中的第一个字符处开始
length:长度 正数 - 从 start 参数所在的位置返回 负数 - 从字符串末端返回
encoding:编码 比如:gbk/utf8/gb2312/不填 不同的编码格式对于中文来讲长度不一样,一般配合mb_strlen使用utf8
注意:1)、如果 start 参数是负数且 length 小于或等于 start,则 length 为 0
2)、一般来说仅使用utf8即可
代码实例
$str='33三三three';
echo mb_substr($str,0,3,'utf8').'<br>';
3、str_repeat()
功能:把字符串重复指定次数
语法:str_repeat(string $str,int $repeat )
str:字符串
repeat:重复次数
代码示例
$str='33三三';
echo str_repeat($str,10).'<br>';
4、mb_stropos()
功能:查找字符串在另一字符串中第一次出现的位置(不区分大小写)
语法:mb_stropos(string $str,string $find,int $start,string $encoding)
str:原始字符串
find:查询内容
start:开始搜索位置,默认为0
encoding:编码格式 默认utf8 通常情况下使用utf8
代码示例
$str='33三二一';
echo mb_strpos($str,"一",0).'<br>';
echo mb_strpos($str,"一",0,'utf8').'<br>';
echo mb_strpos($str,"一",0,'gbk').'<br>';
5、substr_replace()
功能:把字符串的一部分替换为另一个字符串
语法:substr_replace(string $str,string $replacement,int $start,int $length)
str:字符串信息
replacement:需要替换的字符串
start:开始位置
length:长度
echo substr_replace("天气晚来秋","沉",3,3);
注:当为替换汉字时,需要注意一个汉字三个字节
4、根据二维数组中的某一个字段值进行排序
$rs = [
'id'=>1,'name'=>'小张','is_sign'=>5,
'id'=>2,'name'=>'小陈','is_sign'=>3,
'id'=>3,'name'=>'小李','is_sign'=>2,
'id'=>4,'name'=>'小王','is_sign'=>4,
];
$last_names = array_column($rs,'is_sign');
array_multisort($last_names,SORT_DESC,$rs);
var_dump($rs);
5、统计一个数组中的元素个数以及统计一个数组中同一元素出现的次数
$r = ['a','b','c'];
dump(count($r));
$b = ['a','b','a','c','a','c','a'];
dump(array_count_values($b));
6、统计两个数组当中相同键值的个数,以及键值去重
public function vvv()
{
$arr1 = [1,2,5,7,10,13,13,13];
$arr2 = [2,4,7,8,13];
$res1=array_intersect($arr1,$arr2);
var_dump($res1);
$count1 = count($res1);
var_dump($count1);
$res2 = array_unique($res1);
var_dump($res2);
var_dump(count($res2));
}
7、判断数组中是否存在一个键值,判断数组中是否存在一个键名
$a = ['nnn','mmm'];
$b = in_array('nnn',$a);
dump($b);
$c = in_array('aaa',$a);
dump($c);
$a = ['nnn'=>'ddd','mmm'=>'fff'];
$b = array_key_exists('nnn',$a);
dump($b);
$c = array_key_exists('aaa',$a);
dump($c);
8、将图片转为base_64位编码格式以及将base64位普通编码转为图片保存到文件中
public function timeceshi()
{
$img = 'http://www.lh.com/uploads/20181119/8760db7392380af20342e07813ad079.jpg';
$base64_img = base64_encode(file_get_contents($img));
$imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
if (strstr($base64_img,",")){
$base64_img = explode(',',$base64_img);
$base64_img = $base64_img[1];
}
$path = "./uploads/code/".date("Ymd",time());
if (!is_dir($path)){
mkdir($path,0777,true);
}
$imageSrc= $path."/". $imageName;
$r = file_put_contents($imageSrc, base64_decode($base64_img));
}
9、方形图片切圆接口操作 具体根据需要进行扩展
private function radiusImg($imgpath) {
$src_img = null;
$ext = pathinfo($imgpath);
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefrompng($imgpath);
break;
default:
$str = $str = file_get_contents($imgpath);
$src_img = imagecreatefromstring($str);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$radius = ($w) / 2;
$img = imagecreatetruecolor($w, $h);
imagesavealpha($img, true);
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $radius;
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
imagesetpixel($img, $x, $y, $rgbColor);
} else {
$y_x = $r;
$y_y = $r;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
$y_x = $w - $r;
$y_y = $r;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
$y_x = $r;
$y_y = $h - $r;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
$y_x = $w - $r;
$y_y = $h - $r;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
}
return $img;
}
10、php 计算字符串内中文数量和特殊字符数量 粗略计算——比较特殊的字符不计算在内
中文计算
public function chineseNum($str)
{
return (strlen($str) - mb_strlen($str, "utf8")) / 2;
}
特殊字符计算
public function englishNum($str)
{
return mb_strlen($str, 'utf8') - ((strlen($str) - mb_strlen($str, 'utf8')) / 2);
}