I need to split my string input into an array at the commas.
我需要将我的字符串输入分割成一个逗号的数组。
How can I go about accomplishing this?
我怎样才能做到这一点呢?
Input:
9,admin@example.com,8
6 个解决方案
#1
401
Try explode:
试着爆炸:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
输出:
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
#2
42
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
foreach($myArray as $my_Array){
echo $my_Array.'<br>';
}
Output
输出
9
admin@example.com
8
#3
28
$string = '9,admin@google.com,8';
$array = explode(',', $string);
For more complicated situations, you may need to use preg_split
.
对于更复杂的情况,您可能需要使用preg_split。
#4
28
If that string comes from a csv file, I would use fgetcsv()
(or str_getcsv()
if you have PHP V5.3). That will allow you to parse quoted values correctly. If it is not a csv, explode()
should be the best choice.
如果该字符串来自csv文件,我将使用fgetcsv()(或者如果有PHP V5.3,则使用str_getcsv()))。这将允许您正确地解析引用的值。如果不是csv,那就应该使用explosion()。
#5
1
Code:
代码:
$string = "9,admin@example.com,8";
$array = explode(",", $string);
print_r($array);
$no = 1;
foreach ($array as $line) {
echo $no . ". " . $line . PHP_EOL;
$no++;
};
Online:
在线:
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/pGEAlb" ></iframe>
#6
0
In simple way you can go with explode($delimiter, $string)
;
简单地说,您可以使用explosion ($delimiter, $string);
But in a broad way, with Manual Programming :
但从广义上讲,通过手工编程:
$string = "ab,cdefg,xyx,ht623";
$resultArr = [];
$strLength = strlen($string);
$delimiter = ',';
$j = 0;
$tmp = '';
for ($i = 0; $i < $strLength; $i++) {
if($delimiter === $string[$i]) {
$j++;
$tmp = '';
continue;
}
$tmp .= $string[$i];
$resultArr[$j] = $tmp;
}
Outpou : print_r($resultArr);
Outpou:print_r(resultArr美元);
Array
(
[0] => ab
[1] => cdefg
[2] => xyx
[3] => ht623
)
#1
401
Try explode:
试着爆炸:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
输出:
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
#2
42
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
foreach($myArray as $my_Array){
echo $my_Array.'<br>';
}
Output
输出
9
admin@example.com
8
#3
28
$string = '9,admin@google.com,8';
$array = explode(',', $string);
For more complicated situations, you may need to use preg_split
.
对于更复杂的情况,您可能需要使用preg_split。
#4
28
If that string comes from a csv file, I would use fgetcsv()
(or str_getcsv()
if you have PHP V5.3). That will allow you to parse quoted values correctly. If it is not a csv, explode()
should be the best choice.
如果该字符串来自csv文件,我将使用fgetcsv()(或者如果有PHP V5.3,则使用str_getcsv()))。这将允许您正确地解析引用的值。如果不是csv,那就应该使用explosion()。
#5
1
Code:
代码:
$string = "9,admin@example.com,8";
$array = explode(",", $string);
print_r($array);
$no = 1;
foreach ($array as $line) {
echo $no . ". " . $line . PHP_EOL;
$no++;
};
Online:
在线:
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/pGEAlb" ></iframe>
#6
0
In simple way you can go with explode($delimiter, $string)
;
简单地说,您可以使用explosion ($delimiter, $string);
But in a broad way, with Manual Programming :
但从广义上讲,通过手工编程:
$string = "ab,cdefg,xyx,ht623";
$resultArr = [];
$strLength = strlen($string);
$delimiter = ',';
$j = 0;
$tmp = '';
for ($i = 0; $i < $strLength; $i++) {
if($delimiter === $string[$i]) {
$j++;
$tmp = '';
continue;
}
$tmp .= $string[$i];
$resultArr[$j] = $tmp;
}
Outpou : print_r($resultArr);
Outpou:print_r(resultArr美元);
Array
(
[0] => ab
[1] => cdefg
[2] => xyx
[3] => ht623
)