View Full Version : PHP string
Drunken
September 10th, 2003, 11:12 AM
Heya!
I have a string like this: " 1,2,4,7,9" and want to separate the numbers between the , ... how can i do it ?
thks!
Jubba
September 10th, 2003, 11:19 AM
not sure what you mean.. .how do you want to separate them? with spaces?
$string = str_replace(",", " ", $string);
Drunken
September 10th, 2003, 11:21 AM
If possible to an array or separate one by one ...
Drunken
September 10th, 2003, 11:38 AM
Hy! thks already know how :D
like this
$data="1,2,3,4,5";
$data_array = explode(",",$data);
$cont = count ($data_array); //count array
//will return an array:
echo $data_array[0];
echo $data_array[1];
...
Jubba
September 10th, 2003, 12:50 PM
you could also use split()
awligon
September 10th, 2003, 12:53 PM
explode is the way to go. But You don't need to count the array contents if you don't need to.
$data = '4,3,6,2,7';
$data_array = explode(",",$data);
foreach($data_array as $current){
//do whatever
echo $current;
}
Jubba
September 10th, 2003, 12:55 PM
true, explode is faster... damn me... :sigh:
:trout:
but if you are using regular expressions you should use preg_split()...
awligon
September 10th, 2003, 12:57 PM
hey, easy with the trout Jubba. Don't beat yourself up about it. :P
Jubba
September 10th, 2003, 01:00 PM
eh, I always forget which function is faster and I always suggest the wrong ones...
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.