$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) –