...

View Full Version : Split string and get first 4 characters



meadsy25
01-13-2012, 05:32 PM
I have got a script running where I need to split a string and update parts of it. Would now like to update parts of it depending the value of part of the string.




//$string = "2011-12-13;;1;;0;;3,2011-12-14;;1;;0;;1,2011-12-15;;1;;0;;5,2011-12-16;;1;;0;;1;";

$updatedSets = array();
$updatedString = '';

$sets = explode(',',$string);
foreach($sets as $set) {
$setArray = explode(';;',$set);

//$setArray[2] corresponds to the season
//$setArray[1] corresponds to the price

//echo "<br />".$set;

if($setArray[3] == 1) {
$setArray[2] = $high;
}

array_push($updatedSets,implode(';;',$setArray));

}

$updatedString = implode(',',$updatedSets);




I had managed to get the first 4 characters of
$setArray[0] .

It was somthing like this:



$thisyear=$setArray;
echo substr($thisyear,0,4);



But I am not 100% sure how to tie it in with my script so that I can achieve somthing like the following:




if(($setArray[3] == 1)AND($thisyear == 2012)) {
$setArray[2] = 500;
}



So that only sections of the string which have 2012 in them are updated.

Any pointers would be greatly appreciated.

Cheers
Rich

mlseim
01-13-2012, 07:26 PM
How many strings will there (or could there) be?
And how often do you have to do this checking and updating?

I'm thinking about putting it all into a temporary MySQL table,
doing all of the crunching, and regenerating the table back
into a string. MySQL queries are fast and efficient, and much
easier to do than PHP line-by-line commands.


.

meadsy25
01-13-2012, 07:59 PM
Effectively there will be one string updated manually occasionally.

Its for a calendar system so the string has data for everyday of about 2 years.

Cheers
Rich

Kekke
01-13-2012, 08:10 PM
Effectively there will be one string updated manually occasionally.

Its for a calendar system so the string has data for everyday of about 2 years.

Cheers
Rich

That sounds like a very ineffective way of storing that kind of data.

You should really consider using a database to store the data.

That will not only make it easier, but it will be alot safer aswell.

mlseim
01-13-2012, 08:31 PM
I was going to ask more about how and why the data is stored like it is.
Perhaps it's some existing system they have in place? Maybe Meadsy can
elaborate more on the importance of the data structure and why it has to
be like that.


.

meadsy25
01-13-2012, 08:46 PM
It is in a database, but unfortunately it's stored this way from a previous system.

mlseim
01-13-2012, 10:33 PM
What kind of database is it in?
Stored in some sort of text file?

If I were to attempt to read your database,
how would I do that? Is it on your server, online somehow?

meadsy25
01-13-2012, 11:12 PM
It is just a MySQL table that has a single string in it.

Here's a snippet.




2012-01-13;;1;;400;;2;;3,2012-01-14;;1;;400;;2;;3,2012-01-15;;1;;400;;2;;3,2012-01-16;;1;;400;;2;;0,2012-01-17;;1;;400;;2;;0,2012-01-18;;1;;400;;2;;0,2012-01-19;;1;;400;;2;;0,2012-01-20;;1;;400;;2;;0,2012-01-21;;1;;400;;2;;0,



I know its not the best way to store data but its all I have to work with for now.

mlseim
01-14-2012, 01:41 AM
Describe what you might "find" and "change" ...

Example, you want to find some string and change it ...

What is the name of the table(s) where the string(s) are stored?
What is the current name of your column(s) where that large string is stored.
How many rows is it?

If we know the structure of your MySQL table, maybe there's a better way to do this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum