cut characters from variable

Discussion in 'PHP' started by emi87, Jan 15, 2008.

  1. #1
    Hello,

    I want to cut the last 5 characters from a variable.

    For example if my variables are:

    $word1="keyboard";
    $word2="1234testwordkkdjs";

    when I echo them I wanted to show:
    key for echo $word;
    1234testword for echo $word2;

    Just not to show the last 5 characters from every variable.

    Thank you!
     
    emi87, Jan 15, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
  3. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    matthewrobertbell your link helped me ...thank you very much >:D<
    that's what I was looking for
     
    emi87, Jan 15, 2008 IP
  4. bkflash

    bkflash Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    bkflash, Jan 15, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    explode() is nothing like substr(). :confused:

    How would you do this with explode()?
     
    nico_swd, Jan 15, 2008 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    nico's right , substr is the only easy option
     
    matthewrobertbell, Jan 15, 2008 IP
  7. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #7
    Its not just the easiest option. You cant use explode to do this, plain and simple.
     
    papa_face, Jan 15, 2008 IP
  8. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Use the swirly brace, it's quicker.

    i.e.

    $var = 'test';
    
    echo $var{2};
    
    PHP:
    Output:

    s (3rd character, Starts from 0)

    Jay
     
    jayshah, Jan 15, 2008 IP
  9. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #9
    Thats not what he wants though.
     
    papa_face, Jan 15, 2008 IP
  10. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    matthewrobertbell's link is what I was looking for

    
    <?php
    $var = "question";
    $varwithoutlast5characters = substr("$var", 0, -5);
    echo $varwithoutlast5characters;
    ?>
    
    PHP:
    Thank you all!
     
    emi87, Jan 15, 2008 IP
  11. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #11
    Don't need quotes:
    <?php
    $var = "question";
    $varwithoutlast5characters = substr($var, 0, -5);
    echo $varwithoutlast5characters;
    ?>
    Code (markup):
     
    papa_face, Jan 15, 2008 IP
  12. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Sorry to say that I need quotes ... First time I also had tried without but it doesn't work..it works only like I posted.
    :-??
     
    emi87, Jan 15, 2008 IP
  13. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #13
    You must have done something wrong then because theres no need for the quotes when you're just specifying a variable. It actually slows PHP down if you put the quotes in.
     
    papa_face, Jan 15, 2008 IP
  14. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I know that there's no need for the quotes ...that's why first time I tried without quotes... But I don't know why it doesn't work only with quotes ... anyway I am glad to see it working ..Thank you again for help.
     
    emi87, Jan 15, 2008 IP
  15. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #15
    substr grabs data from a string, explode splits data with a delimiter into arrays.
     
    Kaizoku, Jan 16, 2008 IP
  16. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #16
    If you wanted to break a string into an array of component characters, use preg_split.

    <?php
    $str = 'string';
    $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
    print_r($chars);
    ?>
    PHP:
     
    jayshah, Jan 16, 2008 IP
  17. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #17
    LOL @ the explode option....
     
    HuggyStudios, Jan 17, 2008 IP