array_slice

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )

返回数组中指定下标offset和长度length的子数组切片。

参数说明
设第一个参数数组的长度为num_in。
offset

如果offset是正数且小于length,则返回数组会从offset开始;如果offset大于length,则不操作,直接返回。如果offset是负数,则offset = num_in+offset,如果num_in+offset == 0,则将offset设为0。

length

如果length小于0,那么会将length转为num_in - offset + length;否则,如果offset+length > array_count,则length = num_in - offset。如果处理后length还是小于0,则直接返回。

preserve_keys

默认是false,默认不保留数字键值原顺序,设为true的话会保留数组原来的数字键值顺序。

使用实例

<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
print_r(array_slice($input, 2, -1)); // array(0 => 'c', 1 => 'd');
print_r(array_slice($input, 2, -1, true)); // array(2 => 'c', 1 => 'd');
运行步骤
处理参数:offset、length
移动指针到offset指向的位置
从offset开始,拷贝length个元素到返回数组

运行流程图如下


array_splice

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] )

删除input中从offset开始length个元素,如果有replacement参数的话用replacement数组替换删除掉的元素。

参数说明

array_splice函数中的offset和length参数跟array_slice函数中的用法一样。

replacement

如果这个参数设置了,那么函数将使用replacement数组来替换。
如果offset和length指定了没有任何元素需要移除,那么replacement会被插入到offset的位置。
如果replacement只有一个元素,可以不用array()去包着它。
使用示例

<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input变为 array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input变为 array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input变为 array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input为 array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input为 array("red", "green",
// "blue", "purple", "yellow");
源码解读
 在array_splice中,有这么一段代码:

/* Don't create the array of removed elements if it's not going
* to be used; e.g. only removing and/or replacing elements */
if (return_value_used) { // 如果有用到函数返回值则创建返回数组,否则不创建返回数组
int size = length;
/* Clamp the offset.. */
if (offset > num_in) {
offset = num_in;
} else if (offset < 0 && (offset = (num_in + offset)) < 0) {
offset = 0;
}
/* ..and the length */
if (length < 0) {
size = num_in - offset + length;
} else if (((unsigned long) offset + (unsigned long) length) > (unsigned) num_in) {
size = num_in - offset;
}
/* Initialize return value */
array_init_size(return_value, size > 0 ? size : 0);
rem_hash = &Z_ARRVAL_P(return_value);
}
array_splice函数返回的是被删除的切片。这段代码的意思是,如果array_splice需要返回值,那么才创建返回数组,否则不创建,以免浪费空间。这也是一个编程小技巧,仅当需要的时候才返回。比如在函数中使用$result = array_splice(...),那么return_value_used就是true。

php中常用到的方法有:

/* 防sql注入,xss攻击 (1)*/
function actionClean($str)
{
$str=trim($str);
$str=strip_tags($str);
$str=stripslashes($str);
$str=addslashes($str);
$str=rawurldecode($str);
$str=quotemeta($str);
$str=htmlspecialchars($str);
//去除特殊字符
$str=preg_replace("/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|/", "" , $str);
$str=preg_replace("/\s/", "", $str);//去除空格、换行符、制表符
return $str;
}
//防止sql注入。xss攻击(1)
public function actionFilterArr($arr)
{
if(is_array($arr)){
foreach($arr as $k => $v){
$arr[$k] = $this->actionFilterWords($v);
}
}else{
$arr = $this->actionFilterWords($arr);
}
return $arr;
}
//防止xss攻击
public function actionFilterWords($str)
{
$farr = array(
"/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
"/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
"/select|insert|update|delete|drop|\'|\/\*|\*|\+|\-|\"|\.\.\/|\.\/|union|into|load_file|outfile|dump/is"
);
$str = preg_replace($farr,'',$str);
return $str;
}
//防止sql注入,xss攻击(2)
public function post_check($post) {
if(!get_magic_quotes_gpc()) {
foreach($post as $key=>$val){
$post[$key] = addslashes($val);
}
}
foreach($post as $key=>$val){
//把"_"过滤掉
$post[$key] = str_replace("_", "\_", $val);
//把"%"过滤掉
$post[$key] = str_replace("%", "\%", $val); //sql注入
$post[$key] = nl2br($val);
//转换html
$post[$key] = htmlspecialchars($val); //xss攻击
}
return $post;
}

调用:

//防止sql
$post=$this->post_check($_POST);
//var_dump($post);die;
$u_name=trim($post['u_name']);
$pwd=trim($post['pwd']);
if(empty($u_name)||empty($pwd))
{
exit('字段不能非空');
}
$u_name=$this->actionFilterArr($u_name);
$pwd=$this->actionFilterArr($pwd);
//防止sql注入,xss攻击
$u_name=$this->actionClean(Yii::$app->request->post('u_name'));
$pwd=$this->actionClean(Yii::$app->request->post('pwd'));
$email=$this->actionClean(Yii::$app->request->post('email'));
//防止csrf攻击
$session=Yii::$app->session;
$csrf_token=md5(uniqid(rand(),TRUE));
$session->set('token',$csrf_token);
$session->set('token',time());
//接收数据
if($_POST)
{
if(empty($session->get('token')) && $session->get('token')!=Yii::$app->request->post('token') && (time()-$session->get('token_time'))>30){
exit('csrf攻击');
}
//防止sql
.....

(必须放在接收数据之外)

注意:

表单提交值,为防止csrf攻击,控制器中需要加上:

//关闭csrf
piblic $enableCsrfValidation = false;

php中array_slice和array_splice函数解析方式方法的更多相关文章

  1. [PHP源码阅读]array_slice和array_splice函数

    array_slice和array_splice函数是用在取出数组的一段切片,array_splice还有用新的切片替换原删除切片位置的功能.类似javascript中的Array.prototype ...

  2. JAVA中的四种JSON解析方式详解

    JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...

  3. .NET中常用的几种解析JSON方法

    一.基本概念 json是什么? JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. ...

  4. Android中的三种XML解析方式

    在Android中提供了三种解析XML的方式:SAX(Simple API XML),DOM(Document Objrect Model),以及Android推荐的Pull解析方式.下面就对三种解析 ...

  5. XML解析——Java中XML的四种解析方式

    XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已. XML的解析方式分为四 ...

  6. XML解析——Java中XML的四种解析方式(转载 by 龍清扬)

    XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已. XML的解析方式分为四 ...

  7. Java中XML的四种解析方式(一)

    XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析的方式都是一样的,只不过实现的语法不同而已. XML文档以层级标签的 ...

  8. Matlab中bsxfun和unique函数解析

    一.问题来源 来自于一份LSH代码,记录下来. 二.函数解析 2.1 bsxfun bsxfun是一个matlab自版本R2007a来就提供的一个函数,作用是”applies an element-b ...

  9. oracle中next_day()、last_day()函数解析

    oracle中next_day()函数解析 Sql代码 当前系统时间的下一星期一的时间select   next_day(sysdate,1) from dual NEXT_DAY(date,char ...

随机推荐

  1. shell下时间日期的加减乘除运算

    首先我们先来说说什么是shell下的时间戳: 自1970年1月1日(00:00:00 UTC/GMT)以来的秒数.它也被称为Unix时间戳(Unix Timestam.Unix epoch.POSIX ...

  2. 嘴巴题7 BZOJ1426: 收集邮票

    Time Limit: 1 Sec Memory Limit: 162 MB Submit: 546 Solved: 455 [Submit][Status][Discuss] Description ...

  3. 19-10-18-Y

    ZJ一下: 感觉能拿到的分都拿到了,至于后来改题就缶了 其实是:太tui导致没改好 TJ: T1: 正解是$\mathsf{KMP}$,但是广大群众都用了$\mathsf{hash}$…… 发现了一个 ...

  4. 工控安全入门(五)—— plc逆向初探

    之前我们学习了包括modbus.S7comm.DNP3等等工控领域的常用协议,从这篇开始,我们一步步开始,学习如何逆向真实的plc固件. 用到的固件为https://github.com/ameng9 ...

  5. redis教程(三)-----redis缓存雪崩、缓存穿透、缓存预热

    缓存雪崩 概念 缓存雪崩是由于原有缓存失效(过期),新缓存未到期间.所有请求都去查询数据库,而对数据库CPU和内存造成巨大压力,严重的会造成数据库宕机.从而形成一系列连锁反应,造成整个系统崩溃. 解决 ...

  6. SQLServer-SQLServer2017:SQLServer2017

    ylbtech-SQLServer-SQLServer2017:SQLServer2017 微软推出了首个公共预览版本,并持续带来更新和改进.而今天,微软同时向 Windows.Linux.macOS ...

  7. PAT甲级——A1045 Favorite Color Stripe

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

  8. JavaWeb — Servlet(Server Applet)

    Servlet(Server Applet) 全称Java Servlet,未有中文译文.是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. 狭义的Servle ...

  9. PLSQLDeveloper链接报错 解决办法

    PLSQL Developer 9.06.1665中文破解版 亲们,win7 64位系统现在还没有PLSQLDeveloper可以使用,但是怎么办呢.好的,下面教大家怎么在64位系统下安装PLSQLD ...

  10. oracle数据导入/导出(2)

    Oracle数据导入导出imp/exp 功能:Oracle数据导入导出imp/exp就相当与oracle数据还原与备份. 大多情况都可以用Oracle数据导入导出完成数据的备份和还原(不会造成数据的丢 ...