php 在指定位置插入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8251426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:16:48  来源:igfitidea点击:

Insert string at specified position

phpstring

提问by Alex

Is there a PHP function that can do that?

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

I'm using strposto get the position of a substring and I want to insert a stringafter that position.

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

回答by urmaul

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

http://php.net/substr_replace

http://php.net/substr_replace

回答by Tim Cooper

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

substron PHP Manual

substr在 PHP 手册上

回答by alexdets

Try it, it will work for any number of substrings

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

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

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

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

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

回答by user2958137

Use the stringInsert function rather than the putinplace function. I was using the later function to parse a mysql query. Although the output looked alright, the query resulted in a error which took me a while to track down. The following is my version of the stringInsert function requiring only one parameter.

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

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

回答by xdazz

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

回答by chiliNUT

Just wanted to add something: I found tim cooper's answer very useful, I used it to make a method which accepts an array of positions and does the insert on all of them so here that is:

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

EDIT:Looks like my old function assumed $insertstrwas only 1 character and that the array was sorted. This works for arbitrary character length.

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

回答by Ivijan Stefan Stipi?

I have one my old function for that:

我有一个我的旧功能:

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

This is a small powerful function that performs its job flawlessly.

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

回答by Lance Badger

This was my simple solution too append text to the next line after it found the keyword.

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

$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!!!");

Output:

输出:

This is a test
#FINDME#
Insert this awesome string below findme!!!
Other text and data.

回答by jewelhuq

Simple and another way to solve :

简单的另一种解决方法:

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;

}  

回答by Ivan Ivan

function insSubstr($str, $sub, $posStart, $posEnd){
  return mb_substr($str, 0, $posStart) . $sub . mb_substr($str, $posEnd + 1);
}