Hi,
I have a string in PHP I want to know the count of delimiter used in the string, is there any function in PHP to get this count in a For loop
2,7,10
I want to know the count of , used in the above string
How to get the delimiter count in a For Loop in PHP
Moderators: egami, macek, gesf
try this
may this work for you
Code: Select all
$str = "2,7,10";
$delimiter = ",";
$str_arr = explode($delimiter, $str);
$cnt = count($str_arr) - 1;
-
- New php-forum User
- Posts: 64
- Joined: Thu Mar 14, 2013 6:10 am
thanks for the explanation it really works, now my 2nd step is to pass this no in empid fieldpbs wrote:try this
Code: Select all
$str = "2,7,10"; $delimiter = ","; $str_arr = explode($delimiter, $str); $cnt = count($str_arr) - 1;
may this work for you
I am using a foreach loop to update database with query
here is the code
Code: Select all
$i =1;
for each($str_arr as $element)
{
$sql="Update testtable set empname =$dummy where empid = $str_arr[$i]";
$i++;
echo $i;
echo $sql;
}
Code: Select all
Update testtable set empname ='sam' where empid = 2";
Update testtable set empname ='sam' where empid = 7";
Update testtable set empname ='sam' where empid = 10";
last one is also fired where there is no value
Update testtable set empname ='sam' where empid = "; ERROR
for each($str_arr-1 as $element)
any clue on this issue, why extra query is getting fired
-
- New php-forum User
- Posts: 64
- Joined: Thu Mar 14, 2013 6:10 am
it reduces the array index by 1