php 插入字段_php - 在指定位置插入字符串

php - 在指定位置插入字符串

是否有PHP功能可以做到这一点?

我正在使用strpos获取子字符串的位置,我想在该位置后插入string。

Alex asked 2019-03-07T09:14:28Z

11个解决方案

455 votes

$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0);

[http://php.net/substr_replace]

urmaul answered 2019-03-07T09:14:45Z

67 votes

$str = substr($oldstr, 0, $pos) . $str_to_insert . substr($oldstr, $pos);

PHP手册substr

Tim Cooper answered 2019-03-07T09:15:14Z

8 votes

尝试一下,它适用于任意数量的子串

$string = 'bcadef abcdef';

$substr = 'a';

$attachment = '+++';

//$position = strpos($string, 'a');

$newstring = str_replace($substr, $substr.$attachment, $string);

// bca+++def a+++bcdef

?>

alexdets answered 2019-03-07T09:15:40Z

7 votes

使用stringInsert函数而不是putinplace函数。 我使用后来的函数来解析mysql查询。 虽然输出看起来不错,但是查询导致了一个错误,我花了一些时间来追踪。 以下是我的stringInsert函数版本,只需要一个参数。

function stringInsert($str,$insertstr,$pos)

{

$str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);

return $str;

}

user2958137 answered 2019-03-07T09:16:06Z

3 votes

str_replace($sub_str, $insert_str.$sub_str, $org_str);

xdazz answered 2019-03-07T09:16:24Z

3 votes

我有一个旧功能:

function putinplace($string=NULL, $put=NULL, $position=false)

{

$d1=$d2=$i=false;

$d=array(strlen($string), strlen($put));

if($position > $d[0]) $position=$d[0];

for($i=$d[0]; $i >= $position; $i--) $string[$i+$d[1]]=$string[$i];

for($i=0; $i

return $string;

}

// Explanation

$string='My dog dont love postman'; // string

$put="'"; // put ' on position

$position=10; // number of characters (position)

print_r( putinplace($string, $put, $position) ); //RESULT: My dog don't love postman

这是一个功能强大的小功能,可以完美地完成工作。

Ivijan Stefan Stipić answered 2019-03-07T09:16:46Z

3 votes

这是我的简单解决方案,它在找到关键字之后将文本附加到下一行。

$oldstring = "This is a test\n#FINDME#\nOther text and data.";

function insert ($string, $keyword, $body) {

return substr_replace($string, PHP_EOL . $body, strpos($string, $keyword) + strlen($keyword), 0);

}

echo insert($oldstring, "#FINDME#", "Insert this awesome string below findme!!!");

输出:

This is a test

#FINDME#

Insert this awesome string below findme!!!

Other text and data.

Lance Badger answered 2019-03-07T09:17:19Z

3 votes

只是想添加一些东西:我发现蒂姆库珀的答案非常有用,我用它来制作一个接受一系列位置的方法,并在所有这些方面插入,所以这里是:

编辑:看起来我的旧函数假设$insertstr只有1个字符,并且数组已排序。 这适用于任意字符长度。

function stringInsert($str, $pos, $insertstr) {

if (!is_array($pos)) {

$pos = array($pos);

} else {

asort($pos);

}

$insertionLength = strlen($insertstr);

$offset = 0;

foreach ($pos as $p) {

$str = substr($str, 0, $p + $offset) . $insertstr . substr($str, $p + $offset);

$offset += $insertionLength;

}

return $str;

}

chiliNUT answered 2019-03-07T09:18:00Z

1 votes

简单和另一种解决方法:

function stringInsert($str,$insertstr,$pos)

{

$count_str=strlen($str);

for($i=0;$i

{

$new_str .= $str[$i];

}

$new_str .="$insertstr";

for($i=$pos;$i

{

$new_str .= $str[$i];

}

return $new_str;

}

jewelhuq answered 2019-03-07T09:18:27Z

1 votes

function insSubstr($str, $sub, $posStart, $posEnd){

return mb_substr($str, 0, $posStart) . $sub . mb_substr($str, $posEnd + 1);

}

Ivan Ivan answered 2019-03-07T09:18:45Z

0 votes

这里有奇怪的答案! 您可以使用sprintf [链接到文档]轻松地将字符串插入到其他字符串中。 该功能非常强大,可以处理多个元素和其他数据类型。

$color = 'green';

sprintf('I like %s apples.', $color);

给你字符串

I like green apples.

Sliq answered 2019-03-07T09:19:21Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值