我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

验证字符串是PHP中的二进制数

In PHP the bindec()function can be used to convert a binary number to decimal, but there is no error checking to test if the binary number string is valid. What happens is that something like bindec('1051'); returns 5 instead of an error.

Is there an efficient way to validate if the number is a binary string?

if (preg_match('~^[01]+$~', $num)) {
    // valid binary number
}

This should do:

if(preg_match("/^[0-1]+$/", $var)) { 
   // do something 
} 

近期文章

  • 各位帮我看看这个为啥结果是错的呢
  • VS中C#调试出现断点,球球帮改
  • EDA VHDL notepad++
  • citespace 的timezone是灰色的请问该怎么办呢
  • 为什么这个淡出的时候可以缓慢淡出但淡入的时候一瞬就出来了
  • 出现这种问题是什么情况?
  • 关于#Proteus#的问题,如何解决?
  • python打包后文件太大
  • 急!到底如何操作才能看到字幕格式?
  • 我的if语句中出现意外的T_BOOLEAN_AND错误
  • 为什么输出结果不同?求解答
  • vue+Dplayer实现视频功能不显示弹幕
  • 可变参数到底是什么意思
  • [MySQL]为什么我给polyline字段建立了空间索引,但是好像并没有起作用?
  • pycharm程序运行问题
  • Can't instantiate abstract class MolTestDataset with abstract methods get, len
  • 如何输入一串字符串,计算员工的奖金
  • 关于一道c++多继承的问题
  • pb使用图片控件显示错误怎么改
  • 没有MySQL可以在网站内搜索任何单词吗?

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华