PHP array_splice

1.函数的作用:数组中元素的删除和替代

2.函数的参数:

  @params array  &$array

  @params int      $offset

  @params int      $length

  @params array  $replacement_array

3.例子:

 1 <?php
 2 /**
 3  * http://php.net/manual/zh/function.array-splice.php
 4  * 
 5  * @param $input
 6  * @param $offset
 7  * @param null $length
 8  * @param string $splice
 9  * @return string
10  */
11 function str_splice($input, $offset, $length=null, $splice='')
12 {
13     $input = (string)$input;
14     $splice = (string)$splice;
15     $count = strlen($input);
16 
17     // Offset handling (negative values measure from end of string)
18     if ($offset<0) $offset = $count + $offset;
19 
20     // Length handling (positive values measure from $offset; negative, from end of string; omitted = end of string)
21     if (is_null($length)) $length = $count;
22     elseif ($length < 0)  $length = $count-$offset+$length;
23 
24     // Do the splice
25     return substr($input, 0, $offset) . $splice . substr($input, $offset+$length);
26 }
27 
28 $string = "The fox jumped over the lazy dog.";
29 
30 // Outputs "The quick brown fox jumped over the lazy dog."
31 echo str_splice($string, 4, 0, "quick brown ");

 

posted @ 2017-05-23 08:16  GoodByeZ  阅读(301)  评论(0编辑  收藏  举报