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!
you could also look up explode which is another option, sort of like substr http://www.php.net/explode
Use the swirly brace, it's quicker. i.e. $var = 'test'; echo $var{2}; PHP: Output: s (3rd character, Starts from 0) Jay
matthewrobertbell's link is what I was looking for <?php $var = "question"; $varwithoutlast5characters = substr("$var", 0, -5); echo $varwithoutlast5characters; ?> PHP: Thank you all!
Don't need quotes: <?php $var = "question"; $varwithoutlast5characters = substr($var, 0, -5); echo $varwithoutlast5characters; ?> Code (markup):
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. :-??
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.
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.
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: