...

View Full Version : Splitting Keywords



pootlecat
05-09-2006, 03:04 AM
Hi,

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
etc.

Thanks for any help you can give me!

Kakao
05-09-2006, 01: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().

gsnedders
05-09-2006, 01: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))
trim_array($array[$key]);
else
$array[$key] = trim($value);
}
}

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum