Categories: PHP

substr_replace – PHP String Functions

Syntax :

substr_replace ( string, replacement, start, length );

Description :

substr_replace() function will replace text within a part of a string. As the name suggest, it works similarly as its name.

The substr_replace()  is a built-in function in PHP. The index from the input string in which replacement has to be done needs to be passed in start parameter. Replacement tells the new string. If needed, we can also specify the length up to which replacement is needed.

In case of, an array of strings is passed as a parameter then replacements will occur on each string.

If the start parameter is a negative number and length is less than or equal to start, length becomes 0.

Note : This function is binary-safe.

Parameter :

  • string – This is a Required parameter. It is input string.
  • replacement – This is a Required parameter. This is a replacement value.
  • start – This is a Required parameter. Tells from where to start the replacement in input string.
    • +ve number – Start replacing at the position specified.
    • -ve number – Start replacing at the position specified from the end of a string.
    • 0 (zero) – Start replacing at the first character in string.
  • length – This is an Optional parameter. It tells how many characters is to be replaced in original input string. Default is same as the length of the string.
    • +ve number – Length of string to be replaced.
    • -ve number – How many characters should be left at end of string after replacing.
    • 0 (zero) – Perform insert at the starting of string, instead of replace.

Output :

This will return string or an array with replaced values depending upon the input values.


ChangeLog :

[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
Version, Description
PHP 5.0 , The count parameter was added.
PHP 4.0.5, From this version most of the parameters can be an array.

[/table]


Related articles : str_ireplace(), preg_replace, str_replace().


substr_replace() – PHP Functions Example 1 :
<?php

$strExample = 'TUTORIALWITHUS';
echo "String is : $strExample<hr />\n";

/* These two examples replace all of $strExample with 'mines'. */echo substr_replace($strExample, 'mines', 0) . "<br />\n";
echo substr_replace($strExample, 'mines', 0, strlen($strExample)) . "<br />\n";

?>

In above example ,We have a string “TUTORIALWITHUS”. Now this function will replace the value in the var $strExample with value ‘mines’. Output of above code is as below:

String is : TUTORIALWITHUS<hr />
mines<br />
mines<br />

substr_replace() – PHP Functions Example 2 :
<?php

$strExample = 'TUTORIALWITHUS';
echo "String is : $strExample<hr />\n";

/* Insert 'mines' right at the beginning of $strExample. */echo substr_replace($strExample, 'mines', 0, 0) . "<br />\n";
?>

In above example ,We have a string “TUTORIALWITHUS”. Now this function will insert ‘mines’ at the beginning of string in var $strExample with value ‘mines’. Output of above code is as below:

String is : TUTORIALWITHUS<hr />
minesTUTORIALWITHUS<br />

substr_replace() – PHP Functions Example 3 :
<?php

$strExample = 'TUTORIALWITHUS';
echo "String is : $strExample<hr />\n";

/* These next two replace 'WITHUS' in $strExample mines 'mines'. */echo substr_replace($strExample, 'mines', 10, -1) . "<br />\n";
echo substr_replace($strExample, 'mines', -7, -1) . "<br />\n";

?>

In above example ,We have a string “TUTORIALWITHUS”. Now this function will replace the value in the var $strExample with value ‘mines’. Output of above code is as below:

String is : TUTORIALWITHUS<hr />
TUTORIALWIminesS<br />
TUTORIAminesS<br />

substr_replace() – PHP Functions Example 4 :
<?php

$strExample = 'TUTORIALWITHUS';
echo "String is : $strExample<hr />\n";

/* Delete 'WITHUS' from $strExample. */echo substr_replace($strExample, '', 10, -1) . "<br />\n";
?>

In above example ,We have a string “TUTORIALWITHUS”. Now this function will replace the value in the var $strExample with value ‘mines’. Output of above code is as below:

String is : TUTORIALWITHUS<hr />
TUTORIALWIS<br />

substr_replace() – PHP Functions Example 5 :  Array replacement
<?php
$replace = array("month-1","month-2","month-3");
echo implode("<br>",substr_replace($replace,'string',0,5));
?>

In above example ,We have an array of strings “month-1″,”month-2″,”month-3”. Now this function will replace the value in array month-1 with “string-1”, month-2 with “string-2” value in the string. Output of above code in the browser is as below:

string-1
string-2
string-3

substr_replace() – PHP Functions Example 6 :
<?php
$input1 = array('1-', '2-', '3-');
$replace1 = array("January","Febuary","March");
echo implode("<br>\n",substr_replace($input1, $replace1, 2,0)); 
?>

Output of above code in the browser is as below:

1-January<br>
2-Febuary<br>
3-March


jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

12 months ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago