Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

count chars but not using count_chars

16 views
Skip to first unread message

albert

unread,
Nov 14, 2015, 5:42:24 AM11/14/15
to
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?


Thomas 'PointedEars' Lahn

unread,
Nov 14, 2015, 6:51:39 AM11/14/15
to
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.

Jerry Stuckle

unread,
Nov 14, 2015, 9:09:55 AM11/14/15
to
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
jstu...@attglobal.net
==================

Matthew Carter

unread,
Nov 14, 2015, 11:08:52 PM11/14/15
to
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

Thomas 'PointedEars' Lahn

unread,
Nov 17, 2015, 3:34:11 PM11/17/15
to
Matthew Carter wrote:

> Jerry Stuckle <jstu...@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];

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>

Thomas 'PointedEars' Lahn

unread,
Nov 17, 2015, 3:35:43 PM11/17/15
to
Matthew Carter wrote:

> Jerry Stuckle <jstu...@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.";

0 new messages