PHP String's function substr_replace

PHP String's function substr_replace


Posted in : PHP Posted on : January 7, 2011 at 6:15 PM Comments : [ 0 ]

This section contains the detail about the function substr_replace in PHP.

PHP String's function substr_replace

The function of substr_replace is same as str_replace. Both are used for replacing sub string.

But substr_replace function is different from str_replace. It replaces sub string not by searching, it replaces string according to the starting point, and size given to it. It starts replacing starts from the point give to it and replaces up to the length from the starting point. The syntax is given below :

substr_replace(original string, replacement string, starting point, optional length )

Length is optional in this  function. You can use it without it. In that case, it replaces from starting point to the end of the string.

Example :

<?php
//original string which is going to be replace
$mainString = "Hello Guest Welcome";

//string replacement from 6
$str6 = substr_replace($mainString, "Admin", 6, 5);
//string replacement from 12 Without optional length
$str12 = substr_replace($mainString, "Dasvidaniya", 12);
//string replacement from 12
$str0 = substr_replace($mainString, "Welcome to Devmanuals", 0, 19);
//string replacement from 12 Without optional length
$neg1 = substr_replace($mainString, "Namaste", -7);

//display each string
echo "String: $mainString <br />";
echo "String with replaced string from 6: $str6 <br />";
echo "String with replaced string from 12: $str12<br/>";
echo "String with replaced string from 0: $str0<br/>";
echo "String with replaced string from -1: $neg1<br/>";
?>

Output :

String: Hello Guest Welcome
String with replaced string from 6: Hello Admin Welcome
String with replaced string from 12: Hello Guest Dasvidaniya
String with replaced string from 0: Welcome to Devmanuals
String with replaced string from -1: Hello Guest Namaste

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics