PHP - 在特定字符位置向现有字符串添加字符串

时间:2016-06-13 10:20:34

标签: php

我想在第20个字符位置添加一个字符串(制作一个collapser)。

$link = '<span class="collapse"">'
$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quam earum ex voluptate autem cupiditate tempora neque quis aliquam asperiores ab debitis nemo cumque, fuga quae, esse illum aut aliquid rem."

// Trying to add $link to $string at 20th character position

1 个答案:

答案 0 :(得分:1)

使用substr_replace()替换字符串

中的文本
$link = '<span class="collapse"">';
$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quam earum ex voluptate autem cupiditate tempora neque quis aliquam asperiores ab debitis nemo cumque, fuga quae, esse illum aut aliquid rem.";

echo $newstr = substr_replace($string, $link, 20,0);

<强> DEMO