Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How to get the delimiter count in a For Loop in PHP

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How to get the delimiter count in a For Loop in PHP

Postby stephenrodrics » Fri Aug 23, 2013 5:35 am

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
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 53
Joined: Thu Mar 14, 2013 6:10 am

Re: How to get the delimiter count in a For Loop in PHP

Postby pbs » Fri Aug 23, 2013 8:16 pm

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
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India

Re: How to get the delimiter count in a For Loop in PHP

Postby johnj » Sat Aug 24, 2013 12:08 am

there is a builtin function called substr_count(), take a look at that
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: How to get the delimiter count in a For Loop in PHP

Postby stephenrodrics » Sat Aug 24, 2013 2:07 am

pbs 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


thanks for the explanation it really works, now my 2nd step is to pass this no in empid field

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;
}

what I have noticed is query is getting fired 4 times

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

when I try to do this I get a error

for each($str_arr-1 as $element)

any clue on this issue, why extra query is getting fired
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 53
Joined: Thu Mar 14, 2013 6:10 am

Re: How to get the delimiter count in a For Loop in PHP

Postby johnj » Sat Aug 24, 2013 6:34 am

$str_arr is an array. What does $str_arr - 1 mean?
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: How to get the delimiter count in a For Loop in PHP

Postby stephenrodrics » Mon Aug 26, 2013 4:50 am

it reduces the array index by 1
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 53
Joined: Thu Mar 14, 2013 6:10 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.