Q
替換字符串
3
A
回答
7
$finds = explode('-', "a - b - c");
if (count($finds) == 3) {
$finds[2] = " {$finds[2]}";
}
$finds = implode('-', $finds);
0
子串字符串從第一個破折號的索引處開始,使用strpos
,然後對該字符串的其餘部分執行str_replace
。連接在一起。
1
$str ="a - b - c";
if (substr_count($str,"-")>2){
print preg_replace("/^(.*)-(.*)-(.*)/","\\1-\\2- \\3",$str);
}
+0
如果超過3 – Seaux 2010-03-11 00:32:47
+0
,則不起作用,然後爲其添加檢查。 – ghostdog74 2010-03-11 00:45:51
1
**// User Function to replace string by Occurance**
function str_occ_replace($from,$to,$subject,$occ){
$myArray = explode($from,$subject);
print_r($myArray);
$mystring = '';
$index = 1;
foreach($myArray as $ele){
if($index !== $occ AND $index !== $arraySize)
$mystring .= $ele.$from;
else if($index !== $arraySize)
$mystring .= $ele.$to;
$index++;
} // End of Foreach
return $mystring;
} // End of Function
+0
此代碼完美起作用。 BOL。 – 2017-05-18 07:58:56
相關問題
- 1. 替換字符串字符
- 2. 增強字符串替換不會替換換行字符串
- 3. 替換字符串
- 4. 替換字符串
- 5. 字符串替換
- 6. 替換字符串
- 7. 替換字符串
- 8. 替換字符串
- 9. 替換字符串[]
- 10. 替換字符串
- 11. 替換字符串
- 12. 替換字符串
- 13. 替換字符串
- 14. 替換字符串
- 15. 字符串替換交替
- 16. 替換字符串內的字符串
- 17. 字符串替換與字符串
- 18. 將字符串替換爲字符串
- 19. 字符串替換多個字符串
- 20. 用字符串替換字符串java
- 21. 字符串中的字符串替換
- 22. - 替換不替換字符串「()」
- 23. 替換字符串替換的Java
- 24. 字符串替換()/正則表達式替換 - 替換字符串數組中的字符串?
- 25. 用換行符替換字符串PHPStorm
- 26. 字符串替換換行符
- 27. 替換格式字符串
- 28. javascript字符串替換
- 29. PostgreSQL字符串替換
- 30. 獲取字符串替換
+1 - 發佈了同樣的答覆(現已刪除),但太晚:)。你想糾正你的最後一行。 like,$ result = implode(「 - 」,$ finds); – 2010-03-10 21:46:13
哈哈是啊,我已經不得不編輯BC了,我錯過了第二次參照爆炸哈哈。 – Seaux 2010-03-11 00:31:52