- 闭包函数
$b = 'b';
$qq = function($a) use ($b){
return $a.$b;
};
echo $qq('a'); //打印的是'ab'
use里面的变量一定要是存在的值
is_numeric() 判断一个值是不是数字,返回结果是bool类型
- 取唯一值
function createCode(){
return md5(uniqid(microtime(true),true));
}
- 把excell里面的信息转换成数组
public function test2(Request $request)
{
$request = $request->all();
$text = $request['text'];
$res1 = preg_replace('/\n/','_____',$text);
// 把横线作为分割标识 切分成数组
$res2 = explode('_____',$res1);
// dd($res2);
foreach ($res2 as $res3){
// echo $res3; //此时$res3是每一行的值
$res4 = preg_replace('/\t/','***',$res3);
// dd($res4); //把每一行的值转换成了数组
$res5 = explode('***',$res4);
dd($res5);
}
}
2.取两个数组的键的差集
// array_diff_key($arr1,$arr2); 返回的是$arr1,在$arr2中不同的键的数组
//结果都是$arr1里面的键
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
$a = array_diff_key($array1, $array2);
$b = array_diff_key($array2, $array1);
$aa = array_merge($a,$b);
dd($aa);
3.返回数组中值的差集
// array_diff($arr1,$arr2); //返回的是$arr1数组中的值,跟$arr2数组的值相比后的差集。
//(结果都是$arr1里面的值)
$array1 = array('blue' => 5, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
$aa = array_diff($array1, $array2);
dd($aa);
4.array_map
array array_map ( callable $callback , array $array1 [, array $... ] )
array_map():返回数组,是为 array1 每个元素应用 callback函数之后的数组。 callback 函数形参的数量和传给 array_map() 数组数量,两者必须一样。
使用方式1
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
结果
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
使用方式2,=匿名函数
$func = function($value) {
return $value * 2;
};
print_r(array_map($func, range(1, 5)));
使用方式3,直接把函数当成参数写
$a = array(1, 2, 3, 4, 5);
$b = array_map(function($n){
return $n*$n;
},$a);
dd($b);
使用4,更多的数组
function show_Spanish($n, $m)
{
return("The number $n is called $m in Spanish");
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("show_Spanish", $a, $b);
print_r($c);
// printout of $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
例四还有一种方式
public function test()
{
$arr1= array(1, 2, 3, 4, 5);
$arr2 = array(1, 2, 3, 4, 5);
$res = array_map(function($n,$m){
return $n*$m;
},$arr1,$arr2);
dd($res);
}
得到的结果也是$arr1中的值与$arr2的值相乘的值
array:5 [▼
0 => 1
1 => 4
2 => 9
3 => 16
4 => 25
]
其实这个函数是,又几个参数,就要有几个数组
5.array_walk($arr,'fun',$prefix)
// array_walk()第一个参数是数组,第二个参数是函数。也可以有第三个参数,自定义输出内容
// 作为参数的函数中,第一个是值,第二个是键
$fruits = array( "a" => "orange", "b" => "banana", "c" => "apple");
array_walk($fruits,function($val,$key){
echo $key.':'.$val.'
';
});
$fruits = array( "a" => "orange", "b" => "banana", "c" => "apple");
array_walk($fruits,function($val,$key,$prefix){
echo $key.':'.$prefix.'--'.$val.'
';
},'这是前缀,是第三个参数');
6.compact()
正则
/**
* 判断是否是邮箱
* @param $email
* @return bool
*/
public static function isEmail($email)/*{{{*/
{
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email)) {
return true;
}
return false;
}
/**
* 判断是否是手机号码
* @param $mobile
* @return mixed
*/
public static function isMobile($mobile)
{
$preg_mobile = '/^(130|131|132|133|134|135|136|137|138|139|150|151|152|153|154|155|156|157|158|159|180|181|182|183|184|185|186|187|188|189|170|171)\d{8}$/';
return preg_match($preg_mobile, $mobile);
}
function get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_REAL_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_REAL_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
//检查IP地址
if(!preg_match("/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/", $ip)) {
return '0.0.0.0';
}
return $ip;
}
//显示错误信息,并结束
function display_error_exit($error_msg) {
header("Content-type: text/html; charset=utf-8");
echo '';
exit($error_msg);
}
//清除html代码
function clear_html_code($string) {
$string = trim(addslashes(strip_tags($string)));
return $string;
}
function encoding_html($string) {
$string = trim(addslashes(htmlspecialchars($string)));
return $string;
}
//站点临时关闭
function website_close() {
global $web_site;
if($web_site['close']==1) {
echo "";
exit();
}
}
//地区限制功能(可限制地区和IP)
function website_deny() {
global $C_Patch;
include_once($C_Patch."/ip.php");
include_once($C_Patch."/../cache/dqxz.php");
include_once($C_Patch."/common/commonfun.php");
$client_ip = get_client_ip();
$address = '='.iconv("GB2312","UTF-8",convertip($client_ip,"$C_Patch/"));
foreach ($dqxz as $k => $v) {
if (strpos($address,$v) || strpos('='.$client_ip,$v)) {
display_error_exit("对不起,您所在的地区或IP暂时无法访问,请稍后重试");
}
}
}
//判断手机或电脑
function isMobile()
{
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
{
return true;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_SERVER['HTTP_VIA']))
{
// 找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
// 脑残法,判断手机发送的客户端标志,兼容性有待提高
if (isset ($_SERVER['HTTP_USER_AGENT']))
{
$clientkeywords = array ('nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap',
'mobile'
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
{
return true;
}
}
// 协议法,因为有可能不准确,放到最后判断
if (isset ($_SERVER['HTTP_ACCEPT']))
{
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
{
return true;
}
}
return false;
}
//截图功能用来计算字符长度
function str_leng($str) {
mb_internal_encoding("UTF-8");
return mb_strlen($str)*12;
}