Reputation: 311
I have my tags in line:
word1 word2 word3 "word4 word1" word4 "word7 word4" "word67 word56 word1" word7
need to get everything in array like:
word1
word2
word3
word4 word1
word4
word7 word4
word67 word56 word1
word7
need to do it with explode(" ",$input) and explode("\"",$input) combination, or something else, but i have no idea how..
Upvotes: 0
Views: 98
Reputation: 12039
I suggest you to use preg_match_all()
. An Example for you
$str = 'word1 word2 word3 "word4 word1" word4 "word7 word4" "word67 word56 word1" word7';
preg_match_all('/(word\d)|("([^"]*)")/', $str, $m);
print '<pre>';
print_r($m[0]);
print '</pre>';
Upvotes: 0
Reputation: 4265
If you don't want to go down the preg_split route, you could so explode(" \"", $input), and then go through that output, and where the array item doesn't have a trailing ", split that by space. Where it does have the trailing ", remove it.
It's not an elegant solution, but would work.
$round1 = explode(" \"", $input); // split based on the " at the start
$round2 = array();
foreach ($round1 as $word) {
if (substr($word,-1) == "\"") {
$round2[] = str_replace("\"","",$word); // get rid of the trailing "
}
else {
// need to merge our existing array with the exploded array
$round2 = array_merge($round2, explode(" ", $word);
}
}
Upvotes: 0
Reputation: 655
I think the best way is to use str_getcsv
Example :
var_dump( str_getcsv('word1 word2 word3 "word4 word1" word4 "word7 word4" "word67 word56 word1" word7', ' ', '"'));
Upvotes: 3