2014-12-20 24 views
-3

例子:如何特價之前在舊串4個字符的所有關鍵字的添加新的字符串

$oldstring = "How to Formatqw101-put returns between paragraphsre51-for linebreak add 2 spaces at endfd54-italic_ or **bold**se65-indent code by 4 spaces"; 
$newstring = "<br/>"; 

我想在每一個前4個字符加$ newstring「 - 」所以,當成功這個樣子

How to Format<br/>qw101-put returns between paragraphs<br/>re51-for linebreak add 2 spaces at end<br/>fd54-italic_ or **bold**<br/>se65-indent code by 4 spaces 
+1

檢查出爆炸()函數。 –

+0

qw101是5個字符 – Jasen

回答

0

試試這個:

$oldstring = "How to Formatqw101-put returns between paragraphsre51-for linebreak add 2 spaces at endfd54-italic_ or **bold**se65-indent code by 4 spaces"; 
$arr = explode(' ', $oldstring); 
$res = array(); 
foreach($arr as $item) { 
    if(strpos($item, '-') !== false) { 
     $pos = strpos($item, '-'); 
     $item = substr_replace($item, '<br/>', $pos-4, 0); 
    } 
    $res[] = $item.' '; 
} 
echo implode('', $res); 
+0

謝謝^^我會試試這段代碼。 – W3master

相關問題