Re: [PHP] Count the Number of Elements Using Explode



Hi Alice,

U can simple do:
$nr = ( strlen( $message ) / 2 ) + 1 );
$nr = count( explode( '|', $message );

On Fri, Oct 31, 2008 at 4:32 PM, Daniel Brown <danbrown@xxxxxxx> wrote:

On Fri, Oct 31, 2008 at 11:29 AM, Alice Wei <ajwei@xxxxxxxxxxxxx> wrote:

Hi,

I have a code snippet here as follows:

$message="1|2|3|4|5";
$stringChunks = explode("|", $message);

Is it possible to find out the number of elements in this string? It
seems that I could do things like print_r and find out what is the last
element of $stringChunks is, but is there a way where I can use code from
the two lines and have it print out 5 as the number of elements that
contains after the splitting?

http://php.net/strlen
http://php.net/count

--
</Daniel P. Brown>
http://www.parasane.net/
daniel.brown@xxxxxxxxxxxx || danbrown@xxxxxxx
Ask me about our current hosting/dedicated server deals!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
Thanks,

Diogo Neves
Web Developer @ SAPO.pt by PrimeIT.pt


Relevant Pages

  • Re: Regular expression to read non-commented lines from a file
    ... Mario Winterer wrote: ... > Here's the code snippet that prints all lines that are not comments and ... > The input is a String ... >> from a file which are not comments and do not start with whitespace. ...
    (comp.lang.java.help)
  • Re: DLLInport from a ASP.NET web app using C#
    ... <Managed code snippet begins> ... was using StringBuilder argument in the managed code, ... "Scott Allen" wrote: ... >>As for the case from managed code, the call returns correct string value on ...
    (microsoft.public.dotnet.framework.aspnet)
  • classes wrting/reading to binary file
    ... [code snippet appended at the end.) ... A class has a few string variables with not know length at design ... Now I declare lets say a 1000 of those classes and put them in a vector. ...
    (comp.lang.cpp)
  • Re: Cant get IWbemConfigureRefresher::AddObjectByPath to work with Win32_PerfRawData_PerfProc_Proces
    ... OK following your suggestion I've formatted the string, wstr, in the ... code snippet above as ... where pid is the processID that I'm interested in. ... Manbinder Pal Singh wrote: ...
    (microsoft.public.win32.programmer.wmi)
  • Re: classes wrting/reading to binary file
    ... > [code snippet appended at the end.) ... to modify string objects directly or something like that. ... stream out a class and hope it works. ... What you have to do is what you already did for the vector class. ...
    (comp.lang.cpp)