php中array_slice和array_splice函数解析方式方法

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个元素到返回数组

运行流程图如下

php中array_slice和array_splice函数解析
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;

posted @ 2016-12-14 15:41  3002059249  阅读(5395)  评论(0编辑  收藏  举报