dongyigua4468
2016-02-19 10:16 阅读 299
已采纳

如何使用PHP检查一个字符串是否有逗号分隔值

I need one help.I need to check two type of string value using PHP. I am explaining one example below.

suppose one variable

$sup_id='4' //(datatype-varchar)

$sup_id='4,5,6' //(datatype-varchar)

in above example i have different value in same variable name.Here how can i check that the same variable has one value or different with comma(,) separated.Please help me.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpxpz37157 dpxpz37157 2016-02-19 10:35

    Can be easily done by strpos

    if (strpos($sup_id, ',') !== false) {
      echo "There's a comma in the string...!!!";
    }
    
    点赞 评论 复制链接分享
  • douyujun0152 douyujun0152 2016-02-19 10:20

    You could do the following:

    $str = '4,5,6';
    
    $exploded = explode(',', $str);
    
    if (sizeof($exploded) > 1) {
      echo 'It is split.';
    else {
      echo 'Its not.';
    }
    

    What you're doing is, you are splitting the string into an array based on the , delimiter. So if a , exists, the array will have more than 1 value.

    点赞 评论 复制链接分享

相关推荐