PHP - cut string up into smaller string

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey guys,

long story short I have this string
Code:
[[ Pooh , 2009 , 12 , 12 ],[ Mavis , 2010 , 10 , 31 ],[ Mk1 , 2010 , 6 , 1 ],[ Pooh , 2009 , 12 , 31 ],[ MK1 , 2010 , 2 , 29 ]]

It's a mutli array that i have flattened. This will get bigger in time so the below needs to be automated on any scale.

I'd like to loop though it creating a variable for each instance inbetween [..].

This is what i have so far..

Code:
//Setting counter and book variable for bookings name
$x = 1;
$book = "book";

//Setting first starting point
$start = strpos($bookings, "[", 0);

//while x is less than num_of_bookings create a varaible for each booking with all details inside from string (bookings)
while ($x <= $num_of_bookings){							 
	$end = strpos($bookings, "]", $start);
	echo ("<br>---" . $x . "--- start =" . $start . " end =" . $end);
	${$book . $x} = substr($bookings, $start, $end);
	$start = $end;	
	$x++;
}

//echoing for testing
echo ("<br>" . $x);


//echoing for testing
echo "<br><br>";
echo "-----";
echo "<br>";
echo $book1;
echo "<br>";
echo $book2;
echo "<br>";
echo $book3;
echo "<br>";
echo $book4;
echo "<br>";
echo $book5;
echo "<br>";
echo $book6;
echo "<br>";
echo "-----";

it returns the following;

Code:
-----
[[ Pooh , 2009 , 12 , 12
],[ Mavis , 2010 , 10 , 3
],[ Mavis , 2010 , 10 , 3
],[ Mavis , 2010 , 10 , 3
],[ Mavis , 2010 , 10 , 3
],[ Mavis , 2010 , 10 , 3
-----

Can anyone see where I have gone wrong??

I'm quite new to php and any help will be greatly received!

Thanks all

Blastman :)
 
Out of interest why have you flattened an array - I'm guessing it's coming from outside PHP?


yeah from mysql.

tbh, I know that I'm doing this the long way round, but the way i figger it I'm learning loads in both php and javascript and in the next project i do I'll be doing it the 'correct' way.

Any ideas for the problem I've got listed above??
 
arh!!

you'll have to explain this a bit to me.

What is the above code doing?? Is it taking my string and creating a array with it??
 
You should probably look into json_encode, json_decode, serialize and unserialize. Personally I prefer JSON.

thats how i flattened the array.

I used jon_encode to create a string from the array.

I now want to create a loop to cut out all the data inbetween the [] and assign them a varaible as I've tried above.

any suggestions?
 
cool thanks.

Thing is i really need one variable for each booking, so...

$book1 = [ Mavis , 2009 , 12 , 10 ]
$book2 = .......

and so on...


I could use your code to get that data by calling each part of the booking and cat'em together in to one varaible, but isn't there a simpler way??

If I run in trouble later on I need to be to understand and adjust the code.

I was thinking something along the lines of a for or while loop to get find the first instance of [ and ] and then assign the data in the middle to a varaible (${$book . $x} - where $book = book and $x = 1), then create a new variable in the same way but add 1 to $x and get the next bit of data.

I'm nearly there with what i have above it just gets misalined and pulls the wrong bit.

Please dont take this as me not being greatfull, as I am.

Many thanks

Blastman
 
the end result is i want all the data in a database to be pushed over to javascript.

I planned on get my data from the database then on the server split the enties up into thier own variables and then pass them over to javascript for comparason in a calendar that I've written.

The other thing is that as more bookings are placed, the string will get longer so having an array wont work as i can't code infinate amount of if array[x][0] is null as x keeps getting bigger.

The way i though it would work would simply create as many varaibles as needed and put the correct data in.

I can then count the rows in the array at the very begining and then get javascript to loop creating varaibles and passing data accross untill the array is empty.

I hope I'm making sense!?

So I really do need a little help with a for or while loop.

any suggestions?
 
sorted it.

the function substr uses (string), (start), (length) - not end as i thought.

A simple length = end - start did the job.

Thanks for your efforts.
 
hehe, to be honest, you could've probably got most of the battle using explode. I can appreciate blastman's wanting to become more familiar with PHP before doing things the "right" way, but that seems counterproductive to me. If you want to become familiar with the language, set yourself little tests like this, but don't use it in production as you'll come back to it in 6 months and be like "wtf was I thinking?!". I know this because I've done it before.

I do understand your point, but this site I'm building is for a family member who needs it sooner rather than later so I'm kinda of having to learn on the fly. Hence why I've got the 'just get it working' attudied.

I do fully intend to come back in 6 - 12 months and rewrite as that will teach me a whole lot more!!

Also, I did try the whole json_encode and calling it from js with a URL thing but after a week of messing about I simply couldn't get it to work so i went the long way instead.

Thanks for everyones effort :)
 
Back
Top Bottom