smerny Posted October 8, 2009 Share Posted October 8, 2009 How can I verify if a variable is bin (includes only 1's and 0's and possibly a single decimal point)? Same with octal having 0-7 and hexi having 0-F? Pretty much I want it to work the same as the is_numeric() function does for base 10 numbers Link to comment https://forums.phpfreaks.com/topic/176908-solved-verify-if-var-is-binhexioct/ Share on other sites More sharing options...
corbin Posted October 8, 2009 Share Posted October 8, 2009 Technically every number on a computer is stored in binary, so you won't be able to do it mathematically. When you look at it as a string though, you can do something like: if(preg_match('/^[0-1]+(?:\.[0-1]+)?$/', $number)) { //binary } The patterns for base 8: /^[0-7]+(?:\.[0-7]+)?$/ Hex is a little more complicated since it has letters too: /^[0-9|A-F]+(?:\.[0-9|A-F]+)?$/ Link to comment https://forums.phpfreaks.com/topic/176908-solved-verify-if-var-is-binhexioct/#findComment-932759 Share on other sites More sharing options...
smerny Posted October 8, 2009 Author Share Posted October 8, 2009 Thought regex might be the solution... it's one of the things I have the most trouble trying to understand works great, thanks Link to comment https://forums.phpfreaks.com/topic/176908-solved-verify-if-var-is-binhexioct/#findComment-932765 Share on other sites More sharing options...
corbin Posted October 8, 2009 Share Posted October 8, 2009 No problem . Link to comment https://forums.phpfreaks.com/topic/176908-solved-verify-if-var-is-binhexioct/#findComment-932795 Share on other sites More sharing options...
salathe Posted October 8, 2009 Share Posted October 8, 2009 Hex is a little more complicated since it has letters too: /^[0-9|A-F]+(?:\.[0-9|A-F]+)?$/ The vertical pipe is not necessary, the character class [0-9A-F] will work just as well. Link to comment https://forums.phpfreaks.com/topic/176908-solved-verify-if-var-is-binhexioct/#findComment-932936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.