如何在另一个字符串中插入一个字符串? [英] How to insert a string inside another string?

查看:124
本文介绍了如何在另一个字符串中插入一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只看过功能

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.

有没有可用于在输入字符串中插入字符串的php函数?

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

例如..

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

因此,如果我给插入索引3,它将在最左3个字符后插入"@",而$input变为"abc@def".

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

谢谢

推荐答案

您要查找的是字符串插入,而不是填充.

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

填充将字符串设置为一定的长度(如果尚未设置为该长度),因此,如果将填充长度设为3的"abcdef",那么它已经为3,因此什么也不会发生.

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.

尝试:

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

substr_replace

这篇关于如何在另一个字符串中插入一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆