Page 1 of 1
substr Problem
Posted: Sun Mar 23, 2003 7:41 am
by leebo
Hi
I`m trying to extract numbers out of the database using substr.
Say i have a string in the database 23,34,55,74,23,56,88,5,21,11,8,44
How would i get the numbers split from the , ?
like
$string1 = 23
$string2 = 34
$string3 = 55
$string4 = 74
and so on ???

Posted: Sun Mar 23, 2003 7:57 am
by Gen-ik
You could use something like..
Code: Select all
<?php
$numbers = explode(",", $yourstring); //$yourstring is your string of numbers
?>
..that would give you an array called $numbers with $numbers[0] being the first number in your string, $number[1] being the second and so on.
To get the numbers how you want them you can use..
Code: Select all
<?php
$count = count($numbers);
$i = 0;
while($i < $count)
{
$this_num = $i + 1;
${"string".$this_num} = $numbers[$i];
$i++;
}
?>
...that would give you all the variables you want, so $string1 will hold the first number, $string2 will hold the second number and so on...
Hope this helps.
Posted: Sun Mar 23, 2003 8:12 am
by leebo
Thanks that worked apart from i also need to seperate the character [
How would i go about adding more characters to process ??
Thanks
Posted: Sun Mar 23, 2003 8:18 am
by Gen-ik
I don't see the [ symbol in your post... can you post the entire string you want split up so that I can see what characters are in there.
Posted: Sun Mar 23, 2003 11:56 am
by leebo
the string i`m trying to split is:
[7,9,14,9,"holiday in benidorm"],
[16,5,26,5,"10 days holiday"],
And so on.....Its a calendar script but i need to get the numbers out
$string1 = 7
$string2 = 9
$string3 = 14
$string4 = 9
$firstdate = "$string1/$string2";
$seconddate = "$string3/$string4";
print " I am booked out from $firstdate to $seconddate";
then start on next line.
$string1 = 16
$string2 = 5
so on.........
print " I am also booked out from $firstdate to $seconddate";
Posted: Sun Mar 23, 2003 12:12 pm
by McGruff
I didn't test it, but this should strip out the brackets:
$brackets = array('[', ']');
$numbers = str_replace($brackets, "", $numbers);