T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: 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
::: Sponsored Links :::

PR 4 For This Webpage

Vinaora Logo
Home Today :

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($string0$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 '$string7); 

$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 :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007