Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #15908
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: count chars but not using count_chars |
| Date | 2015-11-17 21:35 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <6999416.9ZOyknUsqj@PointedEars.de> (permalink) |
| References | <n2736a$vt0$3@speranza.aioe.org> <n27f71$ivn$1@dont-email.me> <87mvufucom.fsf@ahungry.com> |
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.
Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll 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