Jump to content

help to split words up which may contain spaces.


Jalz

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.