Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Splitting Keywords

    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!

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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().

  • #3
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Here's some code I did a while ago (could definitely do with some tidying up, though).

    PHP Code:
    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);
        }


  • #4
    New Coder
    Join Date
    Mar 2006
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much - I really appreciate your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •