note 91923 added to function.array-splice

From: Date: Wed, 01 Jul 2009 18:53:49 +0000
Subject: note 91923 added to function.array-splice
Groups: php.notes 
Request: Send a blank email to php-notes+get-157787@lists.php.net to get a copy of this message
Should you want a similar function for splicing strings together, here is a rough equivalent: <?php function str_splice($input, $offset, $length=null, $splice='') { $input = (string)$input; $splice = (string)$splice; $count = strlen($input); // Offset handling (negative values measure from end of string) if ($offset<0) $offset = $count + $offset; // Length handling (positive values measure from $offset; negative, from end of string; omitted = end of string) if (is_null($length)) $length = $count; elseif ($length < 0) $length = $count-$offset+$length; // Do the splice return substr($input, 0, $offset) . $splice . substr($input, $offset+$length); } $string = "The fox jumped over the lazy dog."; // Outputs "The quick brown fox jumped over the lazy dog." echo str_splice($string, 4, 0, "quick brown "); ?> Obviously this is not for cases where all you need to do is a simple search-and-replace. ---- Server IP: 209.41.74.194 Probable Submitter: 67.237.228.46 ---- Manual Page -- http://www.php.net/manual/en/function.array-splice.php Edit -- https://master.php.net/note/edit/91923 Del: integrated -- https://master.php.net/note/delete/91923/integrated Del: useless -- https://master.php.net/note/delete/91923/useless Del: bad code -- https://master.php.net/note/delete/91923/bad+code Del: spam -- https://master.php.net/note/delete/91923/spam Del: non-english -- https://master.php.net/note/delete/91923/non-english Del: in docs -- https://master.php.net/note/delete/91923/in+docs Del: other reasons-- https://master.php.net/note/delete/91923 Reject -- https://master.php.net/note/reject/91923 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157787) next »