substr Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

substr Problem

Post 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 ???

:idea:
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post 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";
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I didn't test it, but this should strip out the brackets:

$brackets = array('[', ']');
$numbers = str_replace($brackets, "", $numbers);
Post Reply