¿Cómo puedo dividir una cadena delimitada por comas en un array en PHP?
Necesito dividir mi entrada de cadena en una matriz en las comas.
¿Cómo puedo lograr esto?
Entrada:
9,[email protected],8
211
Author: SilentGhost, 2009-07-14
6 answers
Intenta explotar :
$myString = "9,[email protected],8";
$myArray = explode(',', $myString);
print_r($myArray);
Salida:
Array
(
[0] => 9
[1] => [email protected]
[2] => 8
)
431
Author: Matthew Groves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-24 11:49:08
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-24 11:49:08
$myString = "9,[email protected],8";
$myArray = explode(',', $myString);
foreach($myArray as $my_Array){
echo $my_Array.'<br>';
}
Salida
9
[email protected]
8
43
Author: Jakir Hossain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-07 10:27:28
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-07 10:27:28
$string = '9,[email protected],8';
$array = explode(',', $string);
Para situaciones más complicadas, es posible que deba usar preg_split
.
29
Author: ceejayoz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-14 14:24:11
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-14 14:24:11
Si esa cadena proviene de un archivo csv, usaría fgetcsv()
(o str_getcsv()
si tiene PHP V5. 3). Eso le permitirá analizar correctamente los valores citados. Si no es un csv, explode()
debería ser la mejor opción.
28
Author: soulmerge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-14 14:32:10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-14 14:32:10
Código:
$string = "9,[email protected],8";
$array = explode(",", $string);
print_r($array);
$no = 1;
foreach ($array as $line) {
echo $no . ". " . $line . PHP_EOL;
$no++;
};
En línea:
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/pGEAlb" ></iframe>
1
Author: antelove,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-25 06:18:31
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-25 06:18:31
De manera sencilla puedes ir con explode($delimiter, $string)
;
Pero de manera amplia, con Programación Manual:
$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);
Array
(
[0] => ab
[1] => cdefg
[2] => xyx
[3] => ht623
)
0
Author: Gautam Rai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-21 12:35:26
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-21 12:35:26