::: T4VN Statistics :::
PHP Scripts : 64
PHP Example : 67
PHP Tutorials : 21
PHP News : 93
Total Coupon : 36
Other Tutorials :
Member : 215
Visitor Online : 4
Today Visit: 287
Total Visitor : 301309
Most Online : 41
String Insertion Functions
Author : sleek
The following functions are used to insert a string into another string.
str_insert() - Insert a single string into another string
str_multi_insert() - Insert any number of strings into another string
PHP Example : <?php
function str_insert ( $insert , $string , $index )
{
$prefix = substr ( $string , 0 , $index );
$suffix = substr ( $string , $index );
return $prefix . $insert . $suffix ;
}
function str_multi_insert ( $inserts , $string )
{
// If the $inserts is not an array or there is nothing to insert, return the original string
if (! is_array ( $inserts ) || count ( $inserts ) == 0 ) {
return $string ;
}
$strlen = strlen ( $string );
// Check for negative indices and handle them properly
foreach ( $inserts as $index => $insert ) {
if ( $index < 0 ) {
$new_index = $strlen + $index ;
// Still less than 0, so just make it 0
if ( $new_index < 0 ) {
$new_index = 0 ;
}
$inserts [ $new_index ] = $insert ;
unset( $inserts [ $index ]);
}
}
ksort ( $inserts );
$new_string = '' ;
$prev_index = 0 ;
foreach ( $inserts as $index => $insert ) {
$tmp = substr ( $string , $prev_index , $index - $prev_index );
$new_string .= $tmp . $insert ;
$prev_index = $index ;
}
$new_string .= substr ( $string , $prev_index );
return $new_string ;
}
?>
Usage Example:
PHP Example : <?php
$string = 'I am a genius.' ;
// Produces the string: 'I am a PHP genius.'
echo str_insert ( 'PHP ' , $string , 7 );
$string = '8002223334' ;
$inserts = array(
0 => '(' ,
3 => ') ' ,
6 => '-'
);
// Produces the string: (800) 222-3334
echo str_multi_insert ( $inserts , $string );
?>
Other Example
Apache-Style Event Log Function
Correct string endings
Template Class
BBCode Using MySQL
::: Resources :::
::: New Templates :::
::: Other Tutorials :::