You will learn how to insert characters into a string at a specific position in PHP. Given a string str and an integer position that describes the index in the original string where the desired string insertStr will be added. This can be achieved using various approaches, such as string concatenation, and the substr_replace() function.
Examples:
Input: str = "Welcome GeeksforGeeks" insertStr = "to " position = 8 Output: "Welcome to GeeksforGeeks" Input: str = "Hello World" insertStr = "Beautiful " position = 6 Output: Hello Beautiful World
Table of Content
1. Using String Concatenation
One way to insert characters into a string is by splitting the string at the desired position, inserting the required string and then concatenating the parts with the new characters.
Example: Implementation of inserting characters in a string at a certain position using string concatenation.
<?php
// To perform string concatenation
function insertChars($str, $insert, $position) {
return substr($str, 0, $position)
. $insert . substr($str, $position);
}
// Given Data
$str = "Hello World";
$insertStr = "Beautiful ";
$position = 6;
$newStr = insertChars($str, $insertStr, $position);
echo $newStr;
?>
Output
Hello Beautiful World
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Using substr_replace() Function
This function is used for replacing a part of a string with another string, and can also be used for insertion. The substr_replace() function takes 4 arguments: the original string, the string to be inserted, the position for insertion, and the length of the substring to be replaced (0 in this case, as we are only inserting), and return the updated string.
Example: Implementation of inserting characters in a string at a certain position using substr_replace() function.
<?php
// Given Data
$str = "Hello World";
$insertStr = "Beautiful ";
$position = 6;
// Using substr_replace() for string insertion
$newStr = substr_replace($str, $insertStr, $position, 0);
echo $newStr;
?>
Output
Hello Beautiful World
Time Complexity: O(1)
Auxiliary Space: O(1)
3. Using mb_substr() Function
The mb_substr() function in PHP is used to handle multibyte strings safely. You can utilize this function to split the string into parts, insert the new string, and concatenate the parts back together.
Example: In this approach, mb_substr() is used to extract two parts of the original string: one from the start to the insertion position and the other from the insertion position to the end. The new string is then concatenated between these two parts, resulting in the desired output.
<?php
$str = "Welcome GeeksforGeeks";
$insertStr = "to ";
$position = 8;
$part1 = mb_substr($str, 0, $position);
$part2 = mb_substr($str, $position);
$result = $part1 . $insertStr . $part2;
echo $result;
?>
Output
Welcome to GeeksforGeeks
Time Complexity: O(1)
Auxiliary Space: O(1)
4.Using the str_split() Function
we are using str_split() Function to insert a string into another string at a specified position is by utilizing the str_split() function. This function splits a string into an array, where each element is a substring of the original string. We can then insert the desired string into the array at the specified position and finally concatenate the array back into a single string.
Example:
<?php
function insertString($str, $insertStr, $position) {
// Split the original string into an array
$array = str_split($str);
// Insert the new string at the desired position
array_splice($array, $position, 0, $insertStr);
// Join the array back into a single string
return implode('', $array);
}
echo insertString("Welcome GeeksforGeeks", "to ", 8) . PHP_EOL;
echo insertString("Hello World", "Beautiful ", 6) . PHP_EOL;
?>
Output:
Welcome to GeeksforGeeks
Hello Beautiful World