php中如何分割文本

str_split()函数是将一个字符串按长度平均分割的
我现在比如有一串文本
1,22,33,44,642,123,543
我需要根据“,”逗号将上面的文本分割,
然后加入到数组
1
22
33
44
642
123
543
一直找不到有这样的函数

PHP用空格分割文本为数组的方法:

php逐行读取文本文件,然后处理空格分隔文本,输出为数组的方法。
文本文档text.txt内容:
1 字段1 字段2 2 字段1 字段2 3 字段1 字段2 4 字段1 字段2
文本和文本之间用空格隔开,用php经过处理,输出为数组,以下是代码:
< php $file = fopen("text.txt", "r") or exit("Unable to open file!");
while(!feof($file)) { $arr = split(' ' , fgets($file)); print_r($arr); } fclose($file); >
输出结果:
Array ( [0] => 1 [1] => 字段1 [2] => 字段2 ) Array ( [0] => 2 [1] => 字段1 [2] => 字段2 ) Array ( [0] => 3 [1] => 字段1 [2] => 字段2 ) Array ( [0] => 4 [1] => 字段1 [2] => 字段2 )
这样就实现了PHP用空格分割文本为数组的方法.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-08-04
$str="1,22,33,44,642,123,543";
$res=explode(",",$str,7);
print_r($res);
你试试。本回答被网友采纳
相似回答