dougehe2022 2017-02-10 04:56
浏览 54

如何检查explode只包含逗号分隔值...? [重复]

This question already has an answer here:

How to check that php explode hold only comma separated integer values.

for example i have a

$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);

and i want to check if the value in $str is 10,20,30 and so on. and cannot contain any of string and words.

I basically populate a drop-down from this $arr and i want to make check if user enter integer values with comma then its show the dropdown else if user enter any other format.

i also use is_int or is_numeric but it cannot solve my problem.

thanks for advance .

</div>
  • 写回答

1条回答 默认 最新

  • duannv2081 2017-02-10 05:00
    关注

    What you're looking for is ctype_digit

    $str= '10,20,30,40,50,60,70,80,90,100';
    $arr = explode(',',$str);
    foreach($arr as $a){
      if(!ctype_digit($a)){
        //Not an int
      }else{
        //is an int
      }
    }
    

    Output:- https://eval.in/734431

    Reference:- http://php.net/ctype_digit

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀