2011-11-24 76 views

回答

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

http://php.net/substr_replace

+17

这应该是被接受的答案。内置函数在任何时候都优先于用户创建的函数。 – Benjam

+8

是的,第四个参数是“0”会导致替换字符串被插入而不覆盖任何原始字符串。 –

+5

明智的答案。但对于懒惰的人(像我),在你的文章中应该有一个简短的解释。 我现在要做,并从php.net copy'n'paste: “当然,如果长度为零,则此函数将在给定的起始偏移量处将替换插入到字符串中。 – Wolfsblvt

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

substr on PHP Manual

+0

我这一行做了一个str_insert功能,更可重复使用。 – djleop

2
str_replace($sub_str, $insert_str.$sub_str, $org_str); 
+0

这不会代替每一次事件吗?另外,它不是基于索引的...... – Ryan

+0

@minitech op说'使用strpos来获取子字符串的位置,所以'substring'先出现。而他并没有说只找到一个位置。 – xdazz

7

试试,它适用于任何数量的子

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

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

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

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

的工作,只是想补充一下:我发现恬库珀的答案是非常有用的,我用它来作它接受位置的数组,所以这里是不会在所有设备上的插入方法:

function stringInsert($str,$pos,$insertstr) 
{ 
    if (!is_array($pos)) 
     $pos=array($pos); 

    $offset=-1; 
     foreach($pos as $p) 
     { 
      $offset++; 
    $str = substr($str, 0, $p+$offset) . $insertstr . substr($str, $p+$offset); 
     } 
    return $str; 
} 
+0

这不起作用。您的抵消假设$ interstr只有1个字符长。您需要根据insertstr长度添加偏移量,从0开始并在substr之后添加。此外,您应该将仓位从低到高排序以使其工作。我在这里更正了你的功能,你可能想编辑你的代码:http://pastebin.com/g9ADpuU4 –

1

我有一个我的老功能:

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 

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

1

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

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

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

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

简单的解决方案。做得好。 – jewelhuq

0

简单而另一种方式来解决:

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; 

} 
-1

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

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

给你字符串

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

'$ posEnd'不需要。问题是关于在指定位置之后插入子字符串。 – Styx

+0

是的,但我为我的项目做了这个功能。我需要在某个地方替换,而在另一个地方插入。我的功能也在为这个问题工作,所以我不明白你为什么减去我? –

相关问题