Jalz Posted June 29, 2010 Share Posted June 29, 2010 Hi Guys, I have a field within the the database that is outputting the following data "no wheat no sugar no pork" as a string. What I would like to do is is break that string down so its more readable with a character which I havent decided but lets say its the pipe, so the output would say no wheat | no sugar | no pork etc. Theres about 12 different options it could be, and I'm guessing I would probably have to use regex to identify them. Anyhelp would be much appreciated. Jalz Quote Link to comment Share on other sites More sharing options...
ldb358 Posted June 29, 2010 Share Posted June 29, 2010 Will they always be in sets of 2 words? Quote Link to comment Share on other sites More sharing options...
Jalz Posted June 29, 2010 Author Share Posted June 29, 2010 Hi ldb358, Short answer is no. The list can contain from 1 to 3 words. I'll list them below, of course this can change depending on the end users. no wheat no sugar no red meat no cheese no dairy no pork vegetarian gluten free no fish no nuts (allergy) Thanks Jalz Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 29, 2010 Share Posted June 29, 2010 The right (and better) way to do this while maintaining some semblance of database normalization, would be to add another table to the database and store a foreign key (fk) record associated with the primary key (id) of the person for each option. PEOPLE id person 1 Bob 2 Sally 3 Josh RESTRICTIONS fk restr 1 no fish 1 no sugar 3 vegetarian 2 no cheese 1 no pork 3 no sugar Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 29, 2010 Share Posted June 29, 2010 Ideally, you should store these values as separate records in an associated table instead of dumping them into a single field. It seems like more work, but it will make your life much easier in the long run. Well, now that you show all the possible options, it is evident that you cannot use a regular expression for this since there is no common rule to apply, if all the options began with "no", then you could use a regular expression: preg_match_all("#(\bno .+?(?=no|$))#", $text, $textToArray); But the options for "vegetarian" and "gluten free" make that impossible. You will have to go with a much more inefficient approach. <?php //Value from database $database_value = "no wheat no sugar gluten free no pork no nuts (allergy) vegetarian"; //Array of all possible options $optionsAry = array('no wheat', 'no sugar', 'no red meat', 'no cheese', 'no dairy', 'no pork', 'vegetarian', 'gluten free', 'no fish', 'no nuts (allergy)'); //parse database string using available options $userOptionsAry = array(); foreach($optionsAry as $optionStr) { if(strpos($database_value, $optionStr)!==false) { $userOptionsAry[] = $optionStr; } } //Convert database options array to string with separator $userOptionsStr = implode('|', $userOptionsAry); echo $userOptionsStr; //Output: no sugar|no pork|vegetarian|gluten free|no nuts (allergy) ?> Quote Link to comment Share on other sites More sharing options...
Jalz Posted June 29, 2010 Author Share Posted June 29, 2010 Thanks Guys, Especially mjdamato for the solution which works. I'll take your and Pikachu2000 advice and as phase two normalize my database so I store each dietary options as records rather than single field. Cheers again Jalz Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.