Home »
Imported messages »
comp.lang.php »
string length
Re: string length [message #185325 is a reply to message #185322] |
Tue, 18 March 2014 16:00 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
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.
|
|
|
Goto Forum:
Current Time: Wed Nov 27 20:30:40 GMT 2024
Total time taken to generate the page: 0.03999 seconds