2016-06-13 165 views
-3

我想在第20個字符位置添加字符串(製作collapser)。PHP - 將字符串添加到特定字符位置的現有字符串

$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 
+4

看看這裏的字符串的一部分中替換文本 - > http://stackoverflow.com/questions/8251426/insert-string -at-specified-position某人已經問過類似的問題 –

回答

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

相關問題