Answer of this question:
How can I get the last 7 characters of a PHP string? - Stack Overflow How can I get the last 7 characters of a PHP string?
shows this statement:
substr($s, -7)
However, if length of $s is smaller than 7, it will return empty string(tested on PHP 5.2.6), e.g.
substr("abcd", -4) returns "abcd"
substr("bcd", -4) returns nothing
Currently, my workaround is
trim(substr(" $s",-4)) // prepend 3 blanks
Is there another elegant way to write substr() so it can be more perfect?
====
EDIT: Sorry for the typo of return value of substr("bcd", -4) in my post. It misguides people here. It should return nothing. I already correct it. (@ 2016/1/29 17:03 GMT+8)