我有一个这样的字符串:
$myString = '1,2,3,4,8,23,433,1234';
我需要提取数组中没有逗号的所有数字,我该怎么做?
使用explode()
. 请参阅文档http://de3.php.net/explode
<?php
$string = '1,2,3,4,8,23,433,1234';
$pattern = '/\d+/';
preg_match_all($pattern, $string, $matches);
print_r($matches);
?>
您可以使用爆炸功能
$myString = '1,2,3,4,8,23,433,1234';
$num=explode(",",$mystring);
现在数字存储为数组元素。
Print_r($num);
您不需要正则表达式。
$myString = '1,2,3,4,8,23,433,1234';
$myArray = explode(',',$myString);
for($i = 0; $i < count($myArray); $i++)
$myArray[$i] = intval($myArray[$i]);
我使用 php explode 来分隔数字,
<?php
$myString = '1,2,3,4,8,23,433,1234';
$myexplode = explode(",", $myString);
foreach ($myexplode as $number) {
echo $number;
echo "<br/>";
}
?>