Voting

: five minus three?
(Example: nine)

The Note You're Voting On

strata_ranger at hotmail dot com
15 years ago
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.

<< Back to user notes page

To Top