How to Insert Characters in a String at a Certain Position in PHP ?
Last Updated :
19 Jul, 2024
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
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
<?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;
?>
OutputHello 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
<?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;
?>
OutputHello 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
<?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;
?>
OutputWelcome 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
<?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
Similar Reads
Insert a character in String at a Given Position Given a string s, a character c and an integer position pos, the task is to insert the character c into the string s at the specified position pos.Examples: Input: s = "Geeks", c = 'A', pos = 3Output: GeeAksInput: s = "HelloWorld", c = '!', pos = 5Output: Hello!WorldTable of Content[Approach-1] Usin
5 min read
How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t
2 min read
How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla
3 min read
How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a
2 min read
How to get string between two characters in PHP ? A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of ContentUsing substr() Method: Using fo
4 min read