www.webdeveloper.com
Results 1 to 6 of 6

Thread: How do I parse a string between two characters?

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372

    How do I parse a string between two characters?

    I have a string:

    "somethingsomething-13-24"

    I want to make two variables out of this: the "13" and the "24". The first set of characters will always be between two dashes (-). The second set will be after the second dash to the end of the string.

    How would I do this with PHP?

    Thanks.

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    PHP Code:
    $string 'somethingsomething-13-24';
    list(
    $one$two) = array_slice(explode('-'$string), -2);
    echo 
    "one: $one, two: $two"// one: 13, two: 24 

  3. #3
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372
    Mindzai, I owe you one. Thanks!

    Best solution ever.

  4. #4
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372
    Actually, let's say that I want to keep the "somethingsomething" also. How would I do that?

  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    PHP Code:
    $string 'somethingsomething-13-24';
    list(
    $something$one$two) = explode('-'$string); 

  6. #6
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372
    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles