如何使用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.
我需要一个帮助。我需要使用PHP检查两种类型的字符串值。 我在下面解释一个例子。 p>
假设一个变量 p>
$ sup_id ='4'//(datatype-varchar)
$ sup_id ='4,5,6'//(datatype-varchar)
code> pre>
在上面的示例中,我在同一变量名中有不同的值。这是怎么回事 我可以检查相同的变量是否有一个值或不同的逗号(, code>)分开。请帮助我。 p>
div>
答
Can be easily done by strpos
if (strpos($sup_id, ',') !== false) {
echo "There's a comma in the string...!!!";
}
答
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.