我怎麼會去和分離號碼12345
弄成這個樣子用PHP:PHP格式分割數爲獨立的HTML標籤
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
我怎麼會去和分離號碼12345
弄成這個樣子用PHP:PHP格式分割數爲獨立的HTML標籤
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
echo preg_replace('((.))', "<span>$1</span>\n", '12345');
嘗試用:
$input = 12345;
foreach (str_split($input) as $char) {
echo '<span>' . $char . '</span>';
}
var arr = explode('', "12345");
然後遍歷throgh它與foreach和回聲(或做任何你想要的)標籤。
<?php
$str="12345";
$str = str_split($str);
foreach ($str as $letter){
echo '<span>'.$letter.'</span>'.PHP_EOL;
}
?>
$str = '12345';
$arr = str_split($str);
foreach ($arr as $char) {
echo '<span>' . $char . '</span>';
}
如果輸入字符串會很大,這樣的事情將是更多的內存友好,並可能更快,因爲它流,而不是開的第一打破它。
$string = "12345";
for ($i = 0, $j = strlen($string); $i < $j; $i++) {
echo "<span>" . $string{$i} . "</span>\n";
}
這是我所希望的。謝謝!! – 2012-02-29 11:18:27