Code: Select all
<?php
/**
* Inserts a string before the index set my $strpos of the $old_string that we pass in.
*
* @author protokol
* @version 1.0
* @date Thu Dec 02 15:00:04 EST 2004
* @param string $insert_string The string to insert into the $old_string
* @param int $strpos The index to insert the string before in the $old_string
* @param string $old_string The string that we want to manipulate
* @return string The newly created string
*/
function str_insert ($insert_string, $strpos, $old_string)
{
$strlen = strlen($old_string);
// If the string position is off the end of the string index, set it to the length of the string
// which would be the last index in the string plus 1. If the position is less than zero, then
// treat that number as the index starting from the end of the string. For example, if $strpos is -1,
// then the strpos we want to reference is the last index of the string. This will place the $insert_string
// before this index, basically placing it right before the last character in the string.
if ($strpos > $strlen) {
$strpos = $strlen;
} else if ($strpos < 0) {
$strpos += $strlen;
}
// Create the first part of the string up to the point we want to insert the new string at
$new_string = substr($old_string, 0, $strpos);
// Tack on the string we are inserting
$new_string .= $insert_string;
// Finish it off by getting the rest of the original string starting at $strpos
$new_string .= substr($old_string, $strpos);
return $new_string;
}
/**
* Inserts strings into the $old_string at specified locations. Works the same way that str_insert()<br>
* does except it allows you to insert multiple strings into multiple locations at the same time.
*
* @author protokol
* @version 1.0
* @date Thu Dec 02 15:00:16 EST 2004
* @param array $new_strings The strings to insert into the $old_string, along with the positions to insert them
* @param string $old_string The string that we want to manipulate
* @return string The newly created string
*/
function str_multi_insert ($new_strings, $old_string)
{
// Since this function needs an array of new strings, just return the old string if we don't have it
if (!is_array($new_strings)) {
return $old_string;
}
$start = 0;
$new_string = '';
// Sort the input array so that we get correct substring indices
ksort($new_strings);
// Run through all of the insert values and create a new string with them
foreach ($new_strings as $strpos => $insert_string) {
// Get the section of the string from the old string
$new_string .= substr($old_string, $start, ($strpos - $start));
// Tack on the new string we want to insert
$new_string .= $insert_string;
// Set the new start position to the current string position
$start = $strpos;
}
// Get the rest of the original string
$new_string .= substr($old_string, $start);
return $new_string;
}
?>Code: Select all
<?php
$string = 'I am a PHP developer.';
$new_string = str_insert(' kickass', 6, $string);
// This gives $new_string the value of:
// I am a kickass PHP developer.
$string = "8007233288";
$insert_strings = array(
0 => '(',
3 => ') ',
6 => '-'
);
$new_string = str_multi_insert($insert_strings, $string);
// This gives $new_string the value of:
// (800) 723-3288
?>