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. 3、mysql读写性能优化方法

    1.当表格特别多的时候,所新建的表格一定注意索引,数据库内部对索引的处理能够很好的优化查询读写性能

  2. python中defaultdict方法的使用

    默认值可以很方便 众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键 ...

  3. 微信小程序之组件的集合(四)

    这个主要是来开发book的这个大模块的,看看如何优雅的开发出booked模块! 一.book模块的创建 这个就很简单了,创建一个大的框架是很简单的 二.组件的编写 (1)wxml组件页面的编码 首先是 ...

  4. pickle序列化一个函数,将fun()存入文件

  5. ROS节点的初始化及退出详解(ros::init、SIGINT、ros::ok、ros::NodeHandle

    https://haoqchen.site/2018/04/28/ROS-node-init/ #include "ros/ros.h" #include <signal.h ...

  6. 基于阿里云安装脚本扩展 之 自动安装mongodb及php扩展

    好久没有发布文章了,有点跟不上当初这个博客的初衷.为了使自己的博客更新不半途而废,今天特意再写了一个自动安装脚本,一样是基于阿里云的服务端安装脚本进行的扩展.闲话不说,直接放代码: #!/bin/ba ...

  7. PAT甲级——A1052 Linked List Sorting

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...

  8. 《DSP using MATLAB》Problem 7.29

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  9. nodejs+express 初学(三)

    Nodejs 的模块,nodejs中每一个js文件都是独立的,不用担心他们中的变量会相互覆盖 模块是 Node.js 应用程序的基本组成部分,文件和模块是一一对应的.换言之,一个Node.js 文件就 ...

  10. swiper 报错 ‘ Can't find variable: Dom7’

    一般报这个错是因为浏览器兼容问题,例如低版本的IE 现在通过npm install swiper 安装的版本都是4.x的 我的解决方法就是安装低版本的swiper, npm install swipe ...