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


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

count chars but not using count_chars

Started byalbert <alstopspam@stopaspam.com>
First post2015-11-14 11:42 +0100
Last post2015-11-17 21:35 +0100
Articles 5 — 4 participants

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


Contents

  count chars but not using count_chars albert <alstopspam@stopaspam.com> - 2015-11-14 11:42 +0100
    Re: count chars but not using count_chars Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-11-14 12:51 +0100
    Re: count chars but not using count_chars Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-14 09:09 -0500
      Re: count chars but not using count_chars Matthew Carter <m@ahungry.com> - 2015-11-14 23:08 -0500
        Re: count chars but not using count_chars Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-11-17 21:35 +0100

#15895 — count chars but not using count_chars

Fromalbert <alstopspam@stopaspam.com>
Date2015-11-14 11:42 +0100
Subjectcount chars but not using count_chars
Message-ID<n2736a$vt0$3@speranza.aioe.org>
If have "Hello World World"
how can count the spaces number?

I founded this
$str = "Hello World World";
$strArray = count_chars($str,1);


but is necessary to create more code for to have the value
foreach ($strArray as $key=>$value)
     {echo "The character ".chr($key)."was found $value time";}

and filter the $key

there isn't a directly function?

[toc] | [next] | [standalone]


#15897

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-11-14 12:51 +0100
Message-ID<3721621.oV0pfa5JrY@PointedEars.de>
In reply to#15895
albert wrote:

> If have "Hello World World"
> how can count the spaces number?
> 
> I founded this
> $str = "Hello World World";
> $strArray = count_chars($str,1);
> 
> 
> but is necessary to create more code for to have the value
> foreach ($strArray as $key=>$value)
>      {echo "The character ".chr($key)."was found $value time";}
> 
> and filter the $key
> 
> there isn't a directly function?

You can use a regular expression to count non-overlapping substrings, which 
can be single characters too:

<http://php.net/preg_match_all>

Please read at least the PHP manual and PHP FAQs before you post here.


You appear to have difficulties expressing yourself in English; know then 
that this is an *international* newsgroup, and you can also use your native 
language here.  The PHP manual is available is several languages, too.

It is also likely that there is a PHP newsgroup in your native language or 
for your home region, which you should prefer then; my newsserver also 
carries the following newsgroups:

<news:cn.comp.lang.php>                         (Mandarin  – 中文)
<news:cz.comp.lang.php>                         (Czech     – Čeština)
<news:de.comp.lang.php>                         (German    – Deutsch)
<news:dk.edb.internet.webdesign.serverside.php> (Danish    – Dansk)
<news:es.comp.lenguajes.php>                    (Spanish   – Español)
<news:fr.comp.lang.php>                         (French    – français)
<news:it.comp.www.php>                          (Italian   – Italiano)
<news:no.it.programmering.php>                  (Norwegian – Norsk)
<news:pl.comp.lang.php>                         (Polish    – Polski)
<news:tw.bbs.comp.lang.php>                     (Mandarin, Taiwan-specific)

I am a regular both in <news:de.comp.lang.php> and here.


Please post here using your real name (at least first name *and* last name); 
it is considered polite.

Your sender address is invalid; please fix this:

<http://www.interhack.net/pubs/munging-harmful/>

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#15900

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-14 09:09 -0500
Message-ID<n27f71$ivn$1@dont-email.me>
In reply to#15895
On 11/14/2015 5:42 AM, albert wrote:
> If have "Hello World World"
> how can count the spaces number?
> 
> I founded this
> $str = "Hello World World";
> $strArray = count_chars($str,1);
> 
> 
> but is necessary to create more code for to have the value
> foreach ($strArray as $key=>$value)
>     {echo "The character ".chr($key)."was found $value time";}
> 
> and filter the $key
> 
> there isn't a directly function?
> 
> 

Not for a specific character, there isn't.  There are several ways of
doing it.  For instance, using count_chars, you could use something like:

  $cts = count_chars($str, 0); // Return counts of all chars
  $spacecnt = $cts[' '];  // Get number of spaces

Alternatively, something like:

$cts = 0;
for ($i = =; i < strlen($str); $i++)
   if ($str[$i] == ' ')
      $cts++;

Or any number of ways.  You could use a regex, as Pointed Head
indicated, but that can get pretty complex for a new programmer pretty
quickly - and really is overkill for something relatively simple.

And BTW - don't worry about Pointed Head's complaints.  He's a
well-known troll in this and several other newsgroups.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

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


#15902

FromMatthew Carter <m@ahungry.com>
Date2015-11-14 23:08 -0500
Message-ID<87mvufucom.fsf@ahungry.com>
In reply to#15900
Jerry Stuckle <jstucklex@attglobal.net> writes:

> On 11/14/2015 5:42 AM, albert wrote:
>> If have "Hello World World"
>> how can count the spaces number?
>> 
>> I founded this
>> $str = "Hello World World";
>> $strArray = count_chars($str,1);
>> 
>> 
>> but is necessary to create more code for to have the value
>> foreach ($strArray as $key=>$value)
>>     {echo "The character ".chr($key)."was found $value time";}
>> 
>> and filter the $key
>> 
>> there isn't a directly function?
>> 
>> 
>
> Not for a specific character, there isn't.  There are several ways of
> doing it.  For instance, using count_chars, you could use something like:
>
>   $cts = count_chars($str, 0); // Return counts of all chars
>   $spacecnt = $cts[' '];  // Get number of spaces
>
> Alternatively, something like:
>
> $cts = 0;
> for ($i = =; i < strlen($str); $i++)
>    if ($str[$i] == ' ')
>       $cts++;
>
> Or any number of ways.  You could use a regex, as Pointed Head
> indicated, but that can get pretty complex for a new programmer pretty
> quickly - and really is overkill for something relatively simple.
>
> And BTW - don't worry about Pointed Head's complaints.  He's a
> well-known troll in this and several other newsgroups.

If you have a somewhat recent version of PHP, this will work:

$str = "Hello World World";
$spaces = count_chars($str,1)[32];

echo "You had {$spaces} spaces.";

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

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


#15908

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-11-17 21:35 +0100
Message-ID<6999416.9ZOyknUsqj@PointedEars.de>
In reply to#15902
Matthew Carter wrote:

> Jerry Stuckle <jstucklex@attglobal.net> writes:
>> Or any number of ways.  You could use a regex, as Pointed Head
>> indicated, but that can get pretty complex for a new programmer pretty
>> quickly - and really is overkill for something relatively simple.
>>
>> And BTW - don't worry about Pointed Head's complaints.  He's a
>> well-known troll in this and several other newsgroups.

You wish.
 
> If you have a somewhat recent version of PHP, this will work:
> 
> $str = "Hello World World";
> $spaces = count_chars($str,1)[32];
> 
> echo "You had {$spaces} spaces.";

Very nice.  Because the count_chars() function is available since PHP 4 [1], 
the more compatible variant is simply

  $str = "Hello World World";
  $stats = count_chars($str, 1);
  $spaces = $stats[32] ?: 0;

  echo "You had {$spaces} spaces.";

Caveat: count_chars() and other byte-related approaches suffice for counting 
ASCII space characters.  But they cannot count multi-byte characters, while 
preg_match_all() can if you use the “u” flag in the expression and have a 
string encoded with UTF-8.

[1] <http://php.net/count_chars>

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail. 

[toc] | [prev] | [standalone]


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


csiph-web