Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13207 > unrolled thread

string length

Started byrichard <noreply@example.com>
First post2014-03-18 10:45 -0400
Last post2014-03-18 22:50 +0000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  string length richard <noreply@example.com> - 2014-03-18 10:45 -0400
    Re: string length Tim Streater <timstreater@greenbee.net> - 2014-03-18 15:11 +0000
      Re: string length "Peter H. Coffin" <hellsop@ninehells.com> - 2014-03-18 10:39 -0500
      Re: string length The Natural Philosopher <tnp@invalid.invalid> - 2014-03-18 15:57 +0000
    Re: string length Gabe <usenet@moojuice.net> - 2014-03-18 15:53 +0000
      Re: string length The Natural Philosopher <tnp@invalid.invalid> - 2014-03-18 16:00 +0000
        Re: string length Tim Streater <timstreater@greenbee.net> - 2014-03-18 17:47 +0000
    Re: string length Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-18 22:50 +0000

#13207 — string length

Fromrichard <noreply@example.com>
Date2014-03-18 10:45 -0400
Subjectstring length
Message-ID<umgkqacqlbwl.1erkg4bj91fbo$.dlg@40tude.net>
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;

[toc] | [next] | [standalone]


#13208

FromTim Streater <timstreater@greenbee.net>
Date2014-03-18 15:11 +0000
Message-ID<180320141511165855%timstreater@greenbee.net>
In reply to#13207
In article <umgkqacqlbwl.1erkg4bj91fbo$.dlg@40tude.net>, richard
<noreply@example.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.

[toc] | [prev] | [next] | [standalone]


#13210

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2014-03-18 10:39 -0500
Message-ID<slrnligq4q.74r.hellsop@nibelheim.ninehells.com>
In reply to#13208
On Tue, 18 Mar 2014 15:11:16 +0000, Tim Streater wrote:
> In article <umgkqacqlbwl.1erkg4bj91fbo$.dlg@40tude.net>, richard
><noreply@example.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."

[toc] | [prev] | [next] | [standalone]


#13211

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-03-18 15:57 +0000
Message-ID<lg9qdc$i09$1@news.albasani.net>
In reply to#13208
On 18/03/14 15:11, Tim Streater wrote:
> In article <umgkqacqlbwl.1erkg4bj91fbo$.dlg@40tude.net>, richard
> <noreply@example.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.

[toc] | [prev] | [next] | [standalone]


#13209

FromGabe <usenet@moojuice.net>
Date2014-03-18 15:53 +0000
Message-ID<MZZVu.45103$eg2.20462@fx25.am4>
In reply to#13207
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().

[toc] | [prev] | [next] | [standalone]


#13212

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-03-18 16:00 +0000
Message-ID<lg9qie$i09$2@news.albasani.net>
In reply to#13209
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.

[toc] | [prev] | [next] | [standalone]


#13213

FromTim Streater <timstreater@greenbee.net>
Date2014-03-18 17:47 +0000
Message-ID<180320141747086943%timstreater@greenbee.net>
In reply to#13212
In article <lg9qie$i09$2@news.albasani.net>, The Natural Philosopher
<tnp@invalid.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

[toc] | [prev] | [next] | [standalone]


#13214

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-03-18 22:50 +0000
Message-ID<lgaik1$sok$1@dont-email.me>
In reply to#13207
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@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web