So here is the problem I am getting a Hex number from a input filed it could looks something like this FFFF, 000CA3, blah blah blah while trim these down is easy just with $var = ltrim('0',$var);
My problem is when the user type in '0000' and such it trims the 开发者_运维问答whole string to nothing. I could certainly do a if statement on that after the trim result check string become null and add a 0 if it does. but is there any other neat trick out there can solve this like in one statement? Thank you very much in advance.
Here's one I can think of:
$var = dechex(hexdec($var));
hexdec()
converts strings to actual numeric values, so any insignificant zeroes are just that — insignificant. dechex()
converts them back to strings once the zeroes are dropped.
If you require uppercase hex A-F digits for some strange reason, just tack on a strtoupper()
call as dechex()
produces hex digits in lowercase:
$var = strtoupper(dechex(hexdec($var)));
精彩评论