Forum Moderators: coopster
Goal: I'd like to merge all post the data in to a single string, separate each piece of post with a comma, and then check the data using regex for some simple patterns. The order does not necessarily matter but that is the way I am initially approaching it. Once complete this single string will be set to a cookie. To my site's visitor this information will be used in a second style sheet that overrides the primary style sheet which will use PHP to later split data in to an array and construct the secondary stylesheet.
The data construct is fairly simple and repetitive...
1.) User selects a selector (range 0-24).
2.) User selects a property (range 0-3).
3.) User selects a value (three characters 0-f ¦¦ six characters 0-f ¦¦ 'none' ¦¦ 'transparent').
The input names are based on the select[] index.
'ce' + selector[] + property[] = value.
Examples...
Selector 9, property 4 = 'ce83' (value is changed to selected value)
Selector 10, property 3 = 'ce92'
Selector 11, property 2 = 'ce101'
Selector 12, property 1 = 'ce110'
Keep in mind that the select index starts at 0 so...
you think 1 and it's 0...
you think 2 and it's 1...
you think 3 and the index is 2 etc.
This is how the data is structured being sent to the server. Maybe it could be handled by the client but we can't trust the client can we? I am open to the idea of merging the data to a single string at the client though I'd still have to run regex checks at the server regardless.
Any way to reduce server load I use a hidden input to tell the server what form is being used. In my mind at least this skips PHP from executing all of the code I will/we'll be working on here if the request isn't a post method and if it is will only execute in the following condition...
if ($_POST['formis'] == "site_editor")
{
}
My Questions essentially are...
1.) Would it be better to merge all the data in to a single string at the server or the client? By default I have been planning to do this at the server.
2.) I already run regex at the client though would it be easier to do regex on a single string or a temporary array? For example the value 'none' would be allowed on every second of four sets of values while color (0-f) values of three or six characters in length or the value 'transparent' would be allowed via the regex every first, third, and fourth value out of every set of four.
- John
2) It probably makes no difference to the regex if it works on an array or a string. It may well be easier to write a regex that works on specific sections of an array. I also think that it would make the code more readable by using an array.
So as with most things what are you trying to make efficient? As if you are looking for ease of coding and maintenance then that is unlikely to be the most efficient from a server resource point of view.
I'm currently thinking about applying iteration when applying various regex checks against the array.
Scenario One
Right now I'm just trying to figure out how to select the first of, second of, third of, and fourth of iterations in the array to apply regex checks.
Step 1.) I take the post data and implode it...
$comma_separated = implode(",", $_POST);
Step 2.) Well, I have to some how select the data.
Essentially I'm dealing with sets of four (each set is associated to a CSS selector in the final style2.css file). So the fifth piece of the now single/imploded string is where a second 'selector' begins, the ninth piece is where the third 'selector' begins, etc...
Here is a partial example of what the imploded data would look like (this would validate once I have the regex/script working as desired and yes empty spaces would be acceptable)...
966,11/001,0c6,ccf,,none,c99,3c9,,03/002,036,transparent,,12/002,0cf,ebef92,0cc,11/001,cc3,fc6
Step 3.) Once I'm able to get to the data to apply the regex then obviously step three would be to apply the regex. If a single error occurs I'll reject all of the post data (to prevent abuse); I don't mind being strict and I'm not going to attempt to appeal to people who attempt to manipulate the post data which would require ten fold the effort then merely using the easy and intuitive GUI I've created that itself creates the post data.
Step 4.) If the regex checks finds no errors then this last step is fairly simple: create a cookie with the string as the value.
So in this scenario my question is how do I select iterations of each set (4 pieces per selector) in the array to then move on to applying my regex checks?
Scenario Two
I could simply avoid initially imploding the data until later when I need to create a string for the cookie and check against the $_POST array. Using this code...
foreach($_POST as $key => $value)
echo $key."=".$value.'<br />';
...here is a simple example of what the data may look like that is being posted...
ce00=966
ce01=11/001
ce02=0c6
ce03=ccf
ce10=
ce11=none
ce12=c99
ce13=3c9
ce20=
ce21=03/002
ce22=036
ce23=transparent
ce30=
ce31=12/002
ce32=0cf
ce33=ebef92
ce40=0cc
ce41=11/001
ce42=cc3
ce43=fc6
So whichever way is more efficient and creates the least load on the server is the most desirable method for me. Suggestions please?
- John