SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    quick regular expression or explode help

    hi everyone..

    I am storing some data in the form of "7-1,16-2" in one field..

    noe I have this in variable

    $x = "7-1,16-2";

    I only want to the values "1,2" the second ones, something like

    $var = "1,2";

    and then I shouold also have an array may be..
    $arr[0] = "1";
    $arr[1] = "2";

    any quick help would be highly appreciated..

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look up substr and strpos

  3. #3
    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)
    Give this a whirl, quick and dirty, but it works.

    PHP Code:
    <?php
    $sX 
    '7-1,16-2';
    $aItems explode('-',str_replace(',','-',$sX));
    print_r($aItems);
    /*
    Array
    (
        [0] => 7
        [1] => 1
        [2] => 16
        [3] => 2
    )

    */
    ?>
    @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.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the replies.
    SilverB - lol, I didnt tihnk of doing it like this, but good quick concept..

    I believe the best way would be with regex, otherwise the Silver's suggestion is good one.. 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
  •