2017-09-17 19 views

回答

0

如果你的邏輯是對的字符串用空格每三個字符分開,你可以使用str_split()implode()之後。

這裏是解決方案:

<?php 
$string = "KATCAT"; 
$result = implode(" ", str_split($string, 3)); 

echo $result; // "KAT CAT" 

下面是另一個例子,更多的字符:

<?php 
$string = "KATCATMA"; 
$result = implode(" ", str_split($string, 3)); 

echo $result; // "KAT CAT MA" 
0

沒有空格或製表符,您可以使用str_split function。在詳細信息您可以訪問php.net - str-split

<?php 

$str = "KATCAT"; 
$arr2 = str_split($str, 3); 

print_r($arr2); 

?> 

輸出是這樣的:

Array 
(
    [0] => KAT 
    [2] => CAT 
) 

如果你會使用爆炸功能,那麼你必須指定,因爲這個功能的必要參數的分隔符。在這裏php.net - explode,你可以詳細檢查它。

<?php 
    $str = 'one|two|three|four'; 

    // positive limit 
    print_r(explode('|', $str)); 
?> 

Array 
(
    [0] => one 
    [2] => two 
    [2] => three 
) 
+0

還有其他更多的解決方案,比如:https://eval.in/863189,但OP沒有正確解釋,所以很難回答。 – C2486

+0

@ user2486是的,我完全同意OP沒有正確解釋,也可能有其他解決方案。 – Harish

相關問題