Categories
PHP

Replace a portion of string by position

How to use substr_replace function to replace a string portion with another string by a specified position and length. You’ll also learn how to use array syntax over a string to access or modify an nth character by its index number.

  1. Replace substrings with substr_replace().
  2. Replace the nth position character in a string with array syntax.

Replace a substring with a different string

<?php
 //Syntax
 substr_replace(
  array|string $string,
  array|string $replace,
  array|int $offset,
  array|int|null $length = null
 ): string|array

The substr_replace() function replaces the part of the input string, it accepts four parameters:

  1. $string: the input string.
  2. $replace: the replacement string.
  3. $offset: the index within the string at which to start the replacement.
    0 means the start of the string and -1 means the end of string (negative offset will begin from the end of string).
  4. $length: (optional) the character length which is to be replaced.
    Negative length represents the number of characters from the end of string at which to stop replacing.
    If no value is given, the substr_replace() removes the text from the $offset to the end of the string.
    If length is zero 0, the $replace value is inserted instead of replacing.

The substr_replace( ) function replaces a substring identified by an index $offset with a replacement $replace string, it returns a copy of the input string with the characters from the position start ($offset) to the end of the string replaced with the replace string. If the optional length ($length) is supplied, only length characters are replaced. The following examples show how substr_replace( ) works:

<?php
 $str = 'i, ii, iii, iv, v';

 echo substr_replace($str, 'III', 7)  ."\n";
 // Prints: i, ii, III

 echo substr_replace($str, 'II', 3, 2)."\n";
 // Prints: i, II, iii, iv, v

 echo substr_replace($str, 'I', 0, 1) ."\n";
 // Prints: I, ii, iii, iv, v

If you don’t know the index number of the substring you want to replace, you can use the substr_replace() function in connection with the strpos() function (see find position of a substring in the string). See following example:

<?php
 $string = 'Welcome to my site';
 $search = 'my site';
 $replace = 'BrainBell.com';
 $length = strlen ($search);
 $pos = strpos($string, $search);
 echo substr_replace($string, $replace, $pos, $length);
 //Prints: 'Welcome to BrainBell.com'

Example: Replacing multiple substrings (array of strings)

<?php
 $str =     ['i', 'ii', 'iii', 'iv', 'v'];
 $replace = ['I', 'II', 'III', 'IV', 'V'];
 
 print_r (
  substr_replace($str, $replace, 0)
 );
 /*Prints:
Array
(
    [0] => I
    [1] => II
    [2] => III
    [3] => IV
    [4] => V
)*/

Example: Insert text at a specific index of string

To insert a string on a specified offset set the fourth parameter value to zero.

<?php
 $str = 'i, ii, iii, iv, v';
 
 //Insert at start
 echo substr_replace($str, 'one, ', 0, 0) . "\n";
 //Prints: one, i, ii, iii, iv, v
 
 //Insert at offset 3
 echo substr_replace($str, 'two, ', 3, 0);
 //Prints: i, two, ii, iii, iv, v

Example: Insert text at a specific index of multiple strings at once

<?php
 $str = ['i', 'ii', 'iii', 'iv', 'v'];
 $replace = ['one:', 'two:', 'three:', 'four:', 'five:'];
 
 print_r (
  substr_replace($str, $replace, 0,0)
 );

 /*Prints: 
Array
(
    [0] => one:i
    [1] => two:ii
    [2] => three:iii
    [3] => four:iv
    [4] => five:v
)*/

Example: Truncate too big text with substr_replace() function:

The following example displays the first 30 characters of a string and inserts three dots after it.

<?php
 $str = 'The following example displays the first 30 characters of a string and inserts three dots after it.';
 echo substr_replace($str, '...', 30);
 //The following example displays...

Replace the nth position character in a string

We previously explained that a string is a sequence of single characters. You can access or modify characters in a string using array syntax. The following example demonstrates how to use array syntax to access string characters:

<?php
 $string = 'BrainBell.com';
 // Print the first character of a string
 echo $string[0]; //Prints 'B';
 // Print the last character of a string
 echo $string[strlen($string)-1]; //Prints 'm'

Example: Using array syntax to modify string characters by index:

<?php
 $string = 'BrainBell.com';

 //Change the first character 'B' to 'b'
 $string[0] = 'b';
 echo $string;
 //Prints: 'brainBell.com'

 //Change the 6th character 'B' on 5th index to 'b'
 $string[5] = 'b';
 echo $string;
 //Prints: 'brainbell.com'

Manipulating substrings: