dqlxtv1452 2017-02-22 06:52
浏览 85
已采纳

需要一个正则表达式来接受大于0和逗号的数值

i have to check a comma separated value in which i have to allow only numeric character greater than 0 and comma below is format that i required

  • -1,2,3,567,76,67,20,10

and below is the error that should not be accept by the regex

  1. 0,23,344 23,
  2. 0,2323,85 34
  3. 344,44,00
  4. sdfs,34,34,

i have used the regex

this one accept all numeric character and comma but also accept 0

return ( ! preg_match("/^([0-9,])+$/i", $quantity)) ? FALSE : TRUE;

this one accept not accept 0 at any place

return ( ! preg_match("/^([1-9,])+$/i", $quantity)) ? FALSE : TRUE;
  • 写回答

3条回答 默认 最新

  • drsfgwuw61488 2017-02-22 08:26
    关注

    For the sake of good readable and maintainable code I'd rather stick to a validation function - something like this:

    function validateCsv($input)
    {
        foreach (str_getcsv($input) as $value) {
            if (!preg_match('#^[\d]+$#', $value)) {
                throw new \Exception('invalid CSV-value ' . $value);
            }
            if ($value <= 0) {
                throw new \Exception('invalid CSV-value ' . $value);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办