SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP string function

    Hi..guys!What is the string function that cut 01,02,03 to 1,2,3?Thanks...

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What do you mean? You want to get rid of the zeroes? Use str_replace('0', '', $string)

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That would turn "10" into "1".

    If the string is in the same format as the example (e.g. separated by commas, no spaces), then you do this:
    PHP Code:
    str_replace(',0'','$string); 
    Because it makes sure that the 0 is first.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem resolved,thanks..my string only has a zero in leading position.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $s '01';
    var_dump($s);
    $s = (int)$s;
    var_dump($s); # No more leading zero 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get it. 01 will always be displayed as 1.

    PHP Code:
    var_dump(01); 
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  7. #7
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    01 will always be 1 anyway. 01 is simply not a number. 0.1 is a float, 01 is trying to be an string. So a quick conversion to an integer will sort that.

    PHP Code:
    var_dump(01); 
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    not if it's in a string.

    Anyway, the problem was resolved in this thread's duplicate...
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's a string full of numbers separated by commas, not an integer.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, seen.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Watch out for this stuff, PHP thinks its a bit if it starts with a 0.

    $a = 09;
    echo $a // echos 0

    $a = 031;
    echo $a // echo's 25 ! (bin: 11111)

    Don't let any confusion in, make it an (int) as said before, just because it contains digits doesn't make it a "number".
    Can you say "Whatasubtlelittlebug" ?

    nod: Jim Plush


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
  •