FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » string length
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
string length [message #185320] Tue, 18 March 2014 14:45 Go to next message
Mr Oldies is currently offline  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 #185321 is a reply to message #185320] Tue, 18 March 2014 15:11 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
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.

--
"I am enclosing two tickets to the first night of my new play; bring a
friend.... if you have one." - GB Shaw to Churchill "Cannot possibly
attend first night, will attend second... if there is one." - Winston
Churchill, in response.
Re: string length [message #185322 is a reply to message #185320] Tue, 18 March 2014 15:53 Go to previous messageGo to next message
Gabriel is currently offline  Gabriel
Messages: 11
Registered: March 2014
Karma: 0
Junior Member
On 18/03/2014 14:45, richard wrote:
> 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;
>

Keep in mind that strlen() does not technically count single printable
characters but rather bytes in a string. If the input to strlen() is a
multibyte encoded string (e.g. UTF8) then one character will actually be
counted as more than 1 by strlen().
Re: string length [message #185323 is a reply to message #185321] Tue, 18 March 2014 15:39 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Tue, 18 Mar 2014 15:11:16 +0000, 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.

Wild-assed guess. Multibyte characters biting his bottom. If I'm right,
this will be even more entertaining than third normal form...

--
85. I will not use any plan in which the final step is horribly
complicated, e.g. "Align the 12 Stones of Power on the sacred altar
then activate the medallion at the moment of total eclipse."
Instead it will be more along the lines of "Push the button."
Re: string length [message #185324 is a reply to message #185321] Tue, 18 March 2014 15:57 Go to previous messageGo to next message
The Natural Philosoph is currently offline  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 #185325 is a reply to message #185322] Tue, 18 March 2014 16:00 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 18/03/14 15:53, Gabe wrote:
> On 18/03/2014 14:45, richard wrote:
>> 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;
>>
>
> Keep in mind that strlen() does not technically count single printable
> characters but rather bytes in a string. If the input to strlen() is a
> multibyte encoded string (e.g. UTF8) then one character will actually be
> counted as more than 1 by strlen().

Also keep in mind that '.' and '..' will appear in readdir() listings.

From the readdir manual entry...


<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
?>


--
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 #185326 is a reply to message #185325] Tue, 18 March 2014 17:47 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <lg9qie$i09$2(at)news(dot)albasani(dot)net>, The Natural Philosopher
<tnp(at)invalid(dot)invalid> wrote:

> Also keep in mind that '.' and '..' will appear in readdir() listings.
>
> From the readdir manual entry...
>
> <?php
> if ($handle = opendir('.')) {
> while (false !== ($entry = readdir($handle))) {
> if ($entry != "." && $entry != "..") {
> echo "$entry\n";
> }
> }
> closedir($handle);
> }
> ?>

$dirh = opendir ($start);
if ($dirh===false)
{
// handle error and return or exit
}

while (true)
{

$nextfile = readdir ($dirh);
if ($nextfile===false) break;

if ($nextfile=='.' || $nextfile=='..') continue;

// Treat nextfile

}


Much easier. Notice how the braces don't get more than one deep
(instead of three).

--
"People don't buy Microsoft for quality, they buy it for compatibility
with what Bob in accounting bought last year. Trace it back - they buy
Microsoft because the IBM Selectric didn't suck much" - P Seebach, afc
Re: string length [message #185329 is a reply to message #185320] Tue, 18 March 2014 22:50 Go to previous message
Denis McMahon is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Nested PHP
Next Topic: weird global issue
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 20:47:55 GMT 2024

Total time taken to generate the page: 0.01932 seconds