My trim() trims to much

Discussion in 'PHP' started by lampie1978, Oct 10, 2006.

  1. #1
    Hi u all,

    I set up a code that trims a variable, but now it trims to much if the trim value is found twice in the variable.
    Don't know what or where the hickup in the code is....

    Here is the code:
    
    $volgnr = ($row -> volgnr);
    $_GET[knsa_nr] = rtrim($_GET[knsa_volgnr_sch], "$volgnr");
    
    PHP:
    e.g. if $volgnr = 8 and $_GET[knsa_volgnr_sch] = 1364988 the rtrim() returns $_GET[knsa_nr] as 13649 where it should return 136498.
    What did i do wrong in my code??

    Thanxs lampie1978
     
    lampie1978, Oct 10, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    mad4, Oct 10, 2006 IP
  3. Ozz

    Ozz Peon

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you just need 6 digits from that $_GET variable just use:

    
    $trim=substr($myvar,0,6);
    
    PHP:
     
    Ozz, Oct 10, 2006 IP
  4. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The problem is rtrim() is greedy and it will remove all characters which match the trim list until it finds one which does not match.

    A less greedy alternative is preg_replace. Try this:

    
    $volgnr = ($row -> volgnr);
    $_GET[knsa_nr] = preg_replace( "/$volgnr$/", '', $_GET[knsa_volgnr_sch], 1);
    PHP:
    I added the count parameter for good measure. If your version of PHP is too old, remove it. It works fine both ways in my tests.
     
    clancey, Oct 10, 2006 IP
  5. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #5
    Also, shouldn't the variables within the square brackets be quoted too?

    Like:

    
    
    $_GET['knsa_nr'];
    
    
    PHP:
     
    BRUm, Oct 10, 2006 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Not exactly..

    You can do:

    
    $_GET['name'];
    $_GET["name"];
    $_GET[$name];
    $_GET[name];
    
    PHP:
    Peace,
     
    Barti1987, Oct 10, 2006 IP
  7. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #7
    Ah right, that's something I didn't know - You learn something new every day :)

    Bit strange if it allows you to do all those though.

    Lee.
     
    BRUm, Oct 11, 2006 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Although those unquoted strings work, it's not a good idea to do that.

    It only works if you don't have a constant already with that name and because the parser is continually checking the constant list for each unquoted string, there is a performance hit, too...
     
    TwistMyArm, Oct 11, 2006 IP
  9. lampie1978

    lampie1978 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Clancey,

    Thanxs the preg_replace() did the trick.
    Didn't know if it could do the trick when $volgnr has double figures, but it works fine.

    The rest also thanxs for your input..
    Lampie1978
     
    lampie1978, Oct 11, 2006 IP
  10. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I am glad I could help. It will work fine with multiple numbers. If $volgnr is 456 and the other number is 99456, it will trim off the 456, leaving 99.
     
    clancey, Oct 11, 2006 IP