2012-02-29 55 views

回答

4
echo preg_replace('((.))', "<span>$1</span>\n", '12345'); 
+0

這是我所希望的。謝謝!! – 2012-02-29 11:18:27

3

嘗試用:

$input = 12345; 
foreach (str_split($input) as $char) { 
    echo '<span>' . $char . '</span>'; 
} 
0
var arr = explode('', "12345"); 

然後遍歷throgh它與foreach和回聲(或做任何你想要的)標籤。

2

隨着str_split()

<?php 
$str="12345"; 
$str = str_split($str); 

foreach ($str as $letter){ 
    echo '<span>'.$letter.'</span>'.PHP_EOL; 
} 
?> 
2
$str = '12345'; 
$arr = str_split($str); 

foreach ($arr as $char) { 
    echo '<span>' . $char . '</span>'; 
} 
0

如果輸入字符串會很大,這樣的事情將是更多的內存友好,並可能更快,因爲它流,而不是開的第一打破它。

$string = "12345"; 

for ($i = 0, $j = strlen($string); $i < $j; $i++) { 
    echo "<span>" . $string{$i} . "</span>\n"; 
}