Background/Problem:
I love and hate regular expressions. I need help with this. Given the following four examples of input values, I need a regular expression that handles splitting all these various situations of input into the word or phrase boundaries desired. When single or double quotes are in the input, I want the split to preserve the quotes and what’s in it (including any spaces).
$input_1 = '"Phrase one" "Phrase two " " Phrase three" " Phrase four "';
$input_2 = 'I want ice cream';
$input_3 = “Make me ‘one’ with everything”;
$input_4 = '+\’Expressions\’ give “me” a -headache';
Desired results:
Given $input_1 as input, I want to have a $keywords array contain the following:
$keywords[0] = '"Phrase one"’;
$keywords[1] = ‘"Phrase two "’;
$keywords[2] = ‘" Phrase three"’;
$keywords[3] = ‘" Phrase four "’;
Given $input_2 as input, I want a $keywords array to contain:
$keywords[0] = 'I’;
$keywords[1] = ‘want’;
$keywords[2] = ‘ice’;
$keywords[3] = ‘cream’;
Given $input_3 as input, I want a $keywords array to contain:
$keywords[0] = 'Make’;
$keywords[1] = ‘me’;
$keywords[2] = “‘one’”;
$keywords[3] = ‘with’;
$keywords[4] = ‘everything’;
Given $input_4 as input, I want a $keywords array to contain:
$keywords[0] = “+’Expressions’”;
$keywords[1] = ‘give’;
$keywords[2] = ‘”me”’;
$keywords[3] = ‘a’;
$keywords[4] = ‘-headache';
Solution requested:
I need a regular expression to split the various input examples into the desired results. Like:
$keywords = preg_split(‘/some expression/’, $user_input);
I just need a good solid expression to work in all these types of input situations.