回答
$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0);
$str = substr($oldstr, 0, $pos) . $str_to_insert . substr($oldstr, $pos);
我这一行做了一个str_insert功能,更可重复使用。 – djleop
试试,它适用于任何数量的子
<?php
$string = 'bcadef abcdef';
$substr = 'a';
$attachment = '+++';
//$position = strpos($string, 'a');
$newstring = str_replace($substr, $substr.$attachment, $string);
// bca+++def a+++bcdef
?>
的工作,只是想补充一下:我发现恬库珀的答案是非常有用的,我用它来作它接受位置的数组,所以这里是不会在所有设备上的插入方法:
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;
}
这不起作用。您的抵消假设$ interstr只有1个字符长。您需要根据insertstr长度添加偏移量,从0开始并在substr之后添加。此外,您应该将仓位从低到高排序以使其工作。我在这里更正了你的功能,你可能想编辑你的代码:http://pastebin.com/g9ADpuU4 –
我有一个我的老功能:
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
这是一个小巧强大的功能,可以完美地执行其工作。
这是我的简单解决方案,它找到关键字后,将文本追加到下一行。
$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;
}
简单的解决方案。做得好。 – jewelhuq
简单而另一种方式来解决:
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;
}
奇怪的答案在这里!您可以使用sprintf [链接到文档]轻松地将字符串插入其他字符串。该功能非常强大,并且可以处理多个元素和其他数据类型。
$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);
}
'$ posEnd'不需要。问题是关于在指定位置之后插入子字符串。 – Styx
是的,但我为我的项目做了这个功能。我需要在某个地方替换,而在另一个地方插入。我的功能也在为这个问题工作,所以我不明白你为什么减去我? –
- 1. 插入在特定位置R中字符串的字符
- 2. 在字符串中的特定位置插入空格
- 3. 将数组插入指定位置的指定位置
- 4. iOS - 如何在字符串内的特定位置插入字符?
- 5. 在指定位置插入元素
- 6. 将字符插入指定索引处的字符串
- 7. 使用JavaScript在另一个字符串的多个位置插入字符串
- 8. jeditorpane插入符号的位置和字符串位置不同步
- 9. 在mongoalchemy中指定连接字符串的位置0.21
- 10. 在字符串数组中的字符串的特定位置
- 11. 字符串::在插入串
- 12. 字符串插入文档用指定的字体
- 13. 试图插入一个字符串到另一个字符串的x位置
- 14. 在字符串中插入字符串
- 15. ActionScript:在随机位置插入文本字符串?
- 16. 如何在字符串(Python 2.7)中的特定位置插入空格?
- 17. jQuery的插入字符在特定位置
- 18. 在一个字符串中插入一个字符在某个位置
- 19. 如何在指定位置搜索字符串引用?
- 20. 不能在StringBuffer对象插入字符串中指定地方
- 21. 如何在字符串的字符串处插入字符串?
- 22. 在特定位置分割字符串
- 23. 将值插入某个位置的字符串中?
- 24. 将字符串插入阵列中某个位置的数组
- 25. Javascript - 将字符串插入可变位置的文本
- 26. 一个字符串在适当位置插入特殊字符使用C#
- 27. Perl正则表达式在特定位置插入/替换字符串
- 28. 在字符数组中的特定位置插入一个字符
- 29. 如何将指数定义为字符串的索引位置
- 30. 如何指定文件中的字符串保存位置
这应该是被接受的答案。内置函数在任何时候都优先于用户创建的函数。 – Benjam
是的,第四个参数是“0”会导致替换字符串被插入而不覆盖任何原始字符串。 –
明智的答案。但对于懒惰的人(像我),在你的文章中应该有一个简短的解释。 我现在要做,并从php.net copy'n'paste: “当然,如果长度为零,则此函数将在给定的起始偏移量处将替换插入到字符串中。 – Wolfsblvt