Php 在指定位置插入字符串

Php 在指定位置插入字符串,php,string,Php,String,有一个PHP函数可以做到这一点吗 我正在使用strpos获取子字符串的位置,我想在该位置之后插入一个string $str = substr($oldstr, 0, $pos) . $str_to_insert . substr($oldstr, $pos); 试试看,它对任意数量的子字符串都有效 <?php $string = 'bcadef abcdef'; $substr = 'a'; $attachment = '+++'; //$positio

有一个PHP函数可以做到这一点吗

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

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

试试看,它对任意数量的子字符串都有效

<?php
    $string = 'bcadef abcdef';
    $substr = 'a';
    $attachment = '+++';

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

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

    // bca+++def a+++bcdef
?>

在上面的代码段中,
$pos
用于函数的
offset
参数

偏移量
如果偏移量为非负,则更换将从 偏移量将偏移量转换为字符串

如果偏移量为负,则替换将从偏移量的第个开始 字符串末尾的字符


我只是想补充一点:我发现tim cooper的答案非常有用,我用它制作了一个方法,它接受一个位置数组,并对所有位置进行插入,因此如下所示:

EDIT:看起来像是我的旧函数假定
$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;
}

我有一个我以前的函数:

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<$d[1]; $i++) $string[$position+$i]=$put[$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
函数putinplace($string=NULL,$put=NULL,$position=false)
{
$d1=$d2=$i=false;
$d=数组(strlen($string),strlen($put));
如果($position>$d[0]),$position=$d[0];
对于($i=$d[0];$i>=$position;$i--)$string[$i+$d[1]]=$string[$i];

对于($i=0;$i这是我的简单解决方案,在找到关键字后将文本追加到下一行

$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.

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

function stringInsert($str,$insertstr,$pos)
{
    $str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
    return $str;
}  

简单的解决方法:

function stringInsert($str,$insertstr,$pos)
{
  $count_str=strlen($str);
  for($i=0;$i<$pos;$i++)
    {
    $new_str .= $str[$i];
    }

    $new_str .="$insertstr";

   for($i=$pos;$i<$count_str;$i++)
    {
    $new_str .= $str[$i];
    }

  return $new_str;

}  
函数stringInsert($str,$insertstr,$pos)
{
$count_str=strlen($str);

对于($i=0;$i这里有奇怪的答案!您可以使用[link to documentation]轻松地将字符串插入到其他字符串中。该函数功能非常强大,还可以处理多个元素和其他数据类型

$color = 'green';
sprintf('I like %s apples.', $color);
给你线

I like green apples.

@minitech的op说,
使用strpos获取子字符串的位置,所以
子字符串是第一位的。他并没有说只找到一个位置。这个无法解释的答案可能会根据使用的输入字符串进行多次替换。这使得这个答案的值很低,对研究人员来说可能是危险的。我做了一个stru Insert函数有了这一行,它更易于重用。这应该是公认的答案。内置函数在任何一天都优先于用户创建的函数。是的,第四个参数为“0”会导致插入重放字符串,而不会覆盖任何原始字符串。答案很好。但对于懒惰的人(如我)也许你的帖子中应该有一个简短的解释。我现在就做,然后从php.net复制'n'paste:“当然,如果长度为零,那么这个函数将在给定的开始偏移量处插入替换到字符串中。”注意:函数substr_replace()不是多字节安全的!您的POS可能是UTF-8字符的中间部分。您可能需要使用@提姆Cooper的解决方案,但是使用MbSub()。。这不起作用。您的偏移量假定$interstr仅为1个字符长。您需要根据insertstr长度添加偏移量,从0开始,然后在substr之后添加。此外,您应该将位置从低到高排序以使其起作用。我在此处更正了您的函数,您可能需要编辑代码:@Matthias谢谢,下次只需编辑即可或者建议编辑,直到2年后我才注意到这一点,
$posEnd
是不必要的。问题是关于在指定位置后插入子字符串。是的,但我为我的项目创建了此函数。我需要在某个位置替换,而在另一个位置只是插入。我的函数也在处理此问题,所以我不明白为什么这么做你减去我?我没有减去这个答案,但它完全无法解释。请仅出于慷慨和教育的目的发布。为什么有人要使用这个答案而不是另一个答案?(我知道答案;但你应该为研究人员回答你的问题。)因为这个解决方案有效,在某些情况下,只有这个解决方案有效。因为当时我正在为我的任务寻找一个解决方案,而那个解决方案并没有解决我的问题。所以我猜将来像我这样的人会为一个相同的问题寻找一个解决方案,我的解决方案会帮助他。也许还可以说明函数是如何实现的使用?这很好。但它不处理编程方法。我们通常将
'I like apples.
作为变量。因此,我们必须首先在字符串中插入
%s
,返回原始问题。此答案忽略OP的示例输入数据/场景。这是对不同问题的正确答案。
$color = 'green';
sprintf('I like %s apples.', $color);
I like green apples.
function insSubstr($str, $sub, $posStart, $posEnd){
  return mb_substr($str, 0, $posStart) . $sub . mb_substr($str, $posEnd + 1);
}