View Full Version : Splitting Keywords

05-09-2006, 02:04 AM

I have a nasty little problem here but I am hoping the solution is easy :)
I have a list of keywords which are space-separated like so:

"black and white" "b&w" alligator gator farmer agriculture pitchfork anthropomorphism

As you can see the first two are enclosed with quotes to indicate they are 'one' keyword. How can I add the keywords into an array so that the values are as follows:

myarray[0] = black and white
myarray[1] = b&w
myarray[2] = alligator

Thanks for any help you can give me!

05-09-2006, 12:46 PM
Your string is csv like. Is it coming from a file? If it is then use fgetcsv(). If not save it to a temp file and then read it with fgetcsv().

05-09-2006, 12:52 PM
Here's some code I did a while ago (could definitely do with some tidying up, though).

function get_tags($string)
$tags = preg_replace('/ ( )+/', ' ', $tags);
preg_match_all('/"(.*)"/msiU', $tags, $multi);
$tags = preg_replace('/"(.*)"/msiU', '', $tags);
$tags = preg_replace('/ ( )+/', ' ', $tags);
$words = explode(' ', $tags);
$tags = array_merge($multi[1], $words);
$tags = array_unique($tags);
return trim_array($tags);

function trim_array(&$array)
while (list($key, $value) = each($array))
if (is_array($value))
$array[$key] = trim($value);

05-17-2006, 09:17 PM
Thanks so much - I really appreciate your help :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum