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


Groups > comp.lang.php > #15900

Re: count chars but not using count_chars

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: count chars but not using count_chars
Date 2015-11-14 09:09 -0500
Organization A noiseless patient Spider
Message-ID <n27f71$ivn$1@dont-email.me> (permalink)
References <n2736a$vt0$3@speranza.aioe.org>

Show all headers | View raw


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
==================

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web