SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Splitting strings

    What if I have a string like these?

    DANIEL JOHNS - GUITARIST - SILVERCHAIR

    And I want to get just the word GUITARIST
    from the string? How I supposed to do it?

    Will I do 2 strpos?

    Actually I tried 2 strpos already but on the second one, the dash is still there. so I got
    the position of the first "-" again, thus generating an error.

    or there is a shorter way to do it?

    Thanks guys

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    You could try this:
    PHP Code:
    $str 'DANIEL JOHNS - GUITARIST - SILVERCHAIR';
    $segm explode('-'$str);
    $word trim($segm[1]); 
    This will get you the second section of the string delimited by -.

    You could also use substr() but then you have to know exactly from which to which position you need to extract the word.

  3. #3
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks LemonJuice! It worked! Solved....

    You guys are the best.


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
  •