Page 1 of 1
PHP Count comma separated items
Posted: Wed Apr 04, 2012 8:37 pm
by cjkeane
Hi everyone,
I have a column of data in mysql (email attachments) listed like so:
042208-FK/doc67834968.pdf[doc67834968.pdf],doc678349689.pdf[doc678349689.pdf]
is there a way in php to strip off everything prior to and including the forward slash and then count the filenames? In the example above, the count would be 2.
I'm not quite sure how to accomplish that. If anyone has any helpful hints, I'd appreciate it. Thanks.
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 8:43 pm
by Celauran
explode() on comma and
count() the resulting array?
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 8:49 pm
by cjkeane
like so?
$string = $result['Attachments'];
$array = explode(',', $string);
$Attachments = count($array);
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 8:50 pm
by Celauran
You tell me. Is that not working?
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 8:51 pm
by cjkeane
not quite. it indicates 0, but there are 2 files in the $result['Attachments']
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 8:57 pm
by Celauran
Code: Select all
$string = '042208-FK/doc67834968.pdf[doc67834968.pdf],doc678349689.pdf[doc678349689.pdf]';
$array = explode(',', $string);
$count = count($array);
var_dump($count);
That gives me 2. Have you checked the value of $result['Attachments']?
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 9:09 pm
by cjkeane
Yes I have.
I've checked the value in the db and echoed $result['Attachments'] field to the screen, and I do get: 042208-FK/doc67834968.pdf[doc67834968.pdf],doc678349689.pdf[doc678349689.pdf], it's just not displaying the count.
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 9:26 pm
by Celauran
Doesn't make much sense that it should work for me and not for you. What about
Code: Select all
var_dump($result['Attachments']); var_dump($array);
after count()?
Re: PHP Count comma separated items
Posted: Wed Apr 04, 2012 9:33 pm
by cjkeane
I figured out where I went wrong. Yes the count works just fine now. Thanks.