Is there a php function that can chop let's say 10 chars of the end开发者_Go百科 of a string without calling strlen ? So I can avoid unnecessary repeating of variable names.
all I know is substr($str,0,strlen($str)-10);
Read the manual: http://php.net/manual/en/function.substr.php.
...
If length is given and is negative, then that many characters will be omitted from the end of string.
...
$rest = substr("abcdef", 0, -1); // returns "abcde"
The documentation for substr
clearly states:
string substr ( string $string , int $start [, int $length ] )
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, false will be returned.
So:
$str = substr($str, 0, -10);
Please, always use the documentation as your first port of call for reference questions. There is no reason at all not to use it.
It's simple: You should also check to subtract length of the $str
if the length is less that your required chars to remove.
substr($str,0,-10);
If you give it a negative length, it'll remove that many characters from the end.
substr($str, 0, -10);
http://sandbox.phpcode.eu/g/4fb74.php
-10 without strlen :)
<?php
$str = "01234567890123456789";
echo substr($str,0,-10);
outputs 0123456789
精彩评论