在指定位置插入字符串


Insert string at specified position

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

我使用strpos来获得子字符串的位置,并且我想在该位置之后插入string

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

http://php.net/substr_replace

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

offset
如果offset不是负数,则替换将从offset字符串中的第个偏移量。

如果偏移为负,则替换将从第字符串末尾的字符。

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

PHP手册上的substr

试试看,它适用于任何数量的子字符串

<?php
    $string = 'bcadef abcdef';
    $substr = 'a';
    $attachment = '+++';
    //$position = strpos($string, 'a');
    $newstring = str_replace($substr, $substr.$attachment, $string);
    // bca+++def a+++bcdef
?>

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

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

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

$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.
str_replace($sub_str, $insert_str.$sub_str, $org_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

这是一个功能强大的小函数,可以完美地执行其工作。

我只是想添加一些东西:我发现tim cooper的答案非常有用,我用它来制作一个方法,接受一个位置数组,并在所有位置上进行插入,所以这里是:

编辑:看起来我的旧函数假设$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;
}

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

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

给你字符串

I like green apples.

简单而另一种解决方法:

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;
}  
function insSubstr($str, $sub, $posStart, $posEnd){
  return mb_substr($str, 0, $posStart) . $sub . mb_substr($str, $posEnd + 1);
}

我使用这两个函数来修改字符串:

function insertToString(string $mainstr,string $insertstr,int $index):string
{
    return substr($mainstr, 0, $index) . $insertstr . substr($mainstr, $index);
}
function deleteFromString(string $mainstr, int $index, int $length = 1):string
{
    return substr($mainstr, 0, $index) . substr($mainstr, $index + $length);
}

享受。。。