SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get first result

    What would be the best way to parse a comma separated list and extract the first result?

    For instance, I have a list of movie genres, like so;

    $list = 'sci-fi & fantasy, action, adventure, drama';

    I want to put sci-fi & fantasy in one grouping and the rest in another, like this;

    main genre: sci-fi & fantasy
    sub-genres: action, adventure, drama

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $list 'sci-fi & fantasy, action, adventure, drama';
    $genres explode(',',$list);
    $genre array_shift($genres); 

  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    PHP Code:
    $list 'sci-fi & fantasy, action, adventure, drama';
    $genres explode(',',$list);
    $genre array_shift($genres); 
    Nice! How do I put the remaining into a string?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    $genres = implode(',',$genres);

  5. #5
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!

  6. #6
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to put these into mysql, but this foreach loop isn't working. Any suggestions?

    PHP Code:
    $additional_genres implode(',',$genres);

    $sql 'INSERT IGNORE movies_genres (mg_genre,mg_mov_id) VALUES';
    foreach(
    $additional_genres as $genre) {
        
    $sql .= sprintf("('%s', %d),",escape($genre),$id);
    }
    $query mysql_query(rtrim($sql',')); 

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    $additional_genres would be the array of sub-genres not a string of them together.

    PHP Code:
    $list 'sci-fi & fantasy, action, adventure, drama';
    $genres explode(',',$list);
    $mainGenre array_shift($genres); 

    $sql 'INSERT IGNORE movies_genres (mg_genre,mg_mov_id) VALUES';
    foreach(
    $genres as $genre) {
        
    $sql .= sprintf("('%s', %d),",escape($genre),$id);
    }
    $query mysql_query(rtrim($sql',')); 

  8. #8
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. I shouldn't have asked for a string. Thanks for the clarification

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You may also want to bear in mind that you'll have leading white-space on the individual entries given your sample data, it's a quick fix though.

    PHP Code:
    <?php
    $genres 
    array_map('trim'explode(','$list));
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    PHP Code:
    $list 'sci-fi & fantasy, action, adventure, drama';
    $genres explode(',',$list);
    $genre array_shift($genres); 
    Thanks for sharing! Even I wanted this.

  11. #11
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    You may also want to bear in mind that you'll have leading white-space on the individual entries given your sample data, it's a quick fix though.

    PHP Code:
    <?php
    $genres 
    array_map('trim'explode(','$list));
    ?>
    Already figured that one out after it bit me

    Thanks!


Bookmarks

Posting Permissions

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