string length [message #185320] |
Tue, 18 March 2014 14:45  |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
|
|
What can I use to count the number of characters in a string?
strlen() does not seem to work properly.
count_chars() does several different things but does not count the total.
In BASIC I would use value=len(a$).
I've had a look at the string functions and don't see a one that does this.
what I'm looking to do is, read a directory and load an array based upon
certain conditions of the file name.
Using strlen and trying to match the file length to a fixed value does not
work for me.
$count=0;
$yr=1959;
while ($yr<=1969){
if ($handle = opendir('audio/'.$yr)) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
$length=strlen($entry);
if ($length==10){
echo "$entry\n";
echo "<br>";
$count=$count+1;
}
}
closedir($handle);
}
$yr++;
}
echo $count;
|
|
|
|
|
|
Re: string length [message #185324 is a reply to message #185321] |
Tue, 18 March 2014 15:57   |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 18/03/14 15:11, Tim Streater wrote:
> In article <umgkqacqlbwl(dot)1erkg4bj91fbo$(dot)dlg(at)40tude(dot)net>, richard
> <noreply(at)example(dot)com> wrote:
>
>> What can I use to count the number of characters in a string?
>> strlen() does not seem to work properly.
>
> IME, it does.
>
>> count_chars() does several different things but does not count the total.
>> In BASIC I would use value=len(a$).
>> I've had a look at the string functions and don't see a one that does
>> this.
>>
>> what I'm looking to do is, read a directory and load an array based upon
>> certain conditions of the file name.
>>
>> Using strlen and trying to match the file length to a fixed value does
>> not
>> work for me.
>
> Suggest you give some examples which fail. You sure there are no
> unprintable characters in the filename?
>
> I've never had a problem with strlen.
>
IIRC and it may be wrong, strlen() gives the number of octets in the
string, which may fail for multi-byte character sets.
mb_strlen() returns the number of characters, if supplied with the
correct encoding.
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|
|
|
Re: string length [message #185329 is a reply to message #185320] |
Tue, 18 March 2014 22:50  |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Tue, 18 Mar 2014 10:45:24 -0400, richard wrote:
> Using strlen and trying to match the file length to a fixed value does
> not work for me.
This is not what the code you have shown is doing. The code you have
shown is checking for the string length of the file name, not the file
length.
As others have suggested, please give examples of data that you behave
this code does not treat as you expect.
If you read the php manual entry for the strlen() function, both the
possible cause of your problem and a potential solution are presented to
you. Did you actually read the manual entry (and I'm not talking about
the user contributed notes either).
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|