Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a string inside another string?

Tags:

string

php

Just looked at function

str_pad($input, $pad_length, $pad_str, [STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH]) 

which helps to pad some string on left, right or on both sides of a given input.

Is there any php function which I can use to insert a string inside an input string?

for example ..

$input = "abcdef"; $pad_str = "@"; 

so if I give insert index 3, it inserts "@" after first 3 left most characters and $input becomes "abc@def".

thanks

like image 518
TigerTiger Avatar asked Sep 03 '09 10:09

TigerTiger


People also ask

How do you add strings to a string in Java?

You can use StringBuilder. insert​(int offset, String str) to achieve this.

How do you add a string to another string in Python?

We can insert the string into another string after splitting the original string into a list using the string. split() function. After the string is converted into the list, we can insert the string at the list's desired index, using the list. insert() function.

Which function is used to insert one string into another string?

insert() is used to insert characters in string at specified position.


Video Answer


1 Answers

You're looking for a string insert, not a padding.

Padding makes a string a set length, if it's not already at that length, so if you were to give a pad length 3 to "abcdef", well it's already at 3, so nothing should happen.

Try:

$newstring = substr_replace($orig_string, $insert_string, $position, 0); 

PHP manual on substr_replace

like image 145
databyss Avatar answered Sep 18 '22 07:09

databyss