$string = "1, 2, 3, 4, 5, 6, 7, 8";
$int = intval(preg_replace('/[^0-9]+/', '', $string), 10);
echo $int; // output will be 12345678
我所需要的輸出將是這樣的:分割字符串
幫我請。
$string = "1, 2, 3, 4, 5, 6, 7, 8";
$int = intval(preg_replace('/[^0-9]+/', '', $string), 10);
echo $int; // output will be 12345678
我所需要的輸出將是這樣的:分割字符串
幫我請。
試試這個..希望它能工作..
The explode()
函數將字符串分解爲一個數組。
$string = "1, 2, 3, 4, 5, 6, 7, 8";
$arr=explode(',',$string);
foreach ($arr as $a) {
echo $a."<br/>";
}
感謝它的工作 –
@Namlifa IT.Q讓我們享受......歡呼和投票謝謝。 –
,如果你只是想表明作爲您的輸出,你可以使用
$string = "1, 2, 3, 4, 5, 6, 7, 8";
$int = preg_replace('/[^0-9]+/', '<br /><br />', $string);
echo $int;
另一種方法(我將使用)
$string = "1, 2, 3, 4, 5, 6, 7, 8";
$int = explode(',', $string);
var_dump($int);
這裏所有的整數值將被分離,並可以使用如你所願
我認爲你已經在想這個了,但是如果真的需要一個正則表達式來捕獲所有的整數,那麼它們就會崩潰。
$string = "1, 2, 3, 4, 5, 6, 7, 8";
preg_match_all('/[0-9]+/', $string, $match);
echo implode("\n\n", $match[0]);
Explod
荷蘭國際集團則implod
ING似乎是一個更簡單的方法,但:
$string = "1, 2, 3, 4, 5, 6, 7, 8";
echo implode("\n\n", explode(', ', $string));
我試過了,但輸出顯示1 2 3 4 5 6 7 8 –
您是否在網絡瀏覽器中? – chris85
<?php
$string = "1, 2, 3, 4, 5, 6, 7, 8";
$int = explode(", ",$string);
foreach ($int as $value) {
echo "$value <br>";
}
?>
輸出
1
2
3
4
5
6
7
8
如果你想PHP只輸出,然後用下面的一行替換代碼的第二行
$string = "1, 2, 3, 4, 5, 6, 7, 8";
$data= explode(',' ,$string);
echo "<pre>".implode("\n",$data)."</pre>";
:請嘗試這個例子
$int = preg_replace('/[^0-9]+/', "\n", $string);
注意第二個參數的雙引號。
但是,如果你只是想在HTML輸出,使用此:
$int = preg_replace('/[^0-9]+/', '<br />', $string);
好運。
可能的重複[如何將逗號分隔的字符串拆分爲PHP中的數組?](http:// stackoverflow。com/questions/1125730/how-can-i-split-a-array-in-php) –