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


Groups > comp.lang.php > #16442

Re: Functions behaving badly: PHP str_split on an empty string

From "Christoph M. Becker" <cmbecker69@arcor.de>
Newsgroups comp.lang.php
Subject Re: Functions behaving badly: PHP str_split on an empty string
Date 2016-02-17 11:53 +0100
Organization solani.org
Message-ID <na1jeb$8hm$1@solani.org> (permalink)
References <na1g0b$va5$1@dont-email.me>

Show all headers | View raw


James Harris wrote:

> I have been using
> 
>   foreach (str_split($text) as $char)
> 
> but found it does not work properly when $text is empty. Unfortunately,
> when the string is empty str_split returns an array with one element.
> That's nasty. :-(

ACK.  However, this behavior is documented[1]:

| If the split_length length [which defaults to 1] exceeds the length
| of string, the entire string is returned as the first (and only)
| array element.

> I've also just realised that str_split works with bytes and not chars.
> 
> I see that [] notation also works with bytes and not chars. Is that right?

Yes, that's right.  There have been attempts to add support for Unicode
in the PHP core, but for several reasons this has not been accomplished
(yet).

> Is there a recommended way to iterate over the characters of a string
> when that string could be single-byte ASCII or multi-byte Unicode?

You can use mb_split() or preg_split() for multibyte character
encodings.  Anyway, you should be aware in which character encoding the
string is encoded (mb_detect_encoding() might be helpful).

However, I suggest you reconsider the approach to iterate over the
characters of a string, because it's most likely rather slow in PHP (for
one, PHP doesn't have a char type as C does, so a character is stored as
string[2]), and there may be better alternatives available (PCRE comes
to mind).

> I guess that seasoned PHP programmers get used to using a subset of the
> language and learn about potential function gotchas such as the one above.

Indeed.  Besides waiting for improvements (and maybe contributing to the
effort), it's always possible to use existing libraries (such as
Patchwork-UTF8[3]) or to write your own. :)

[1] <http://php.net/manual/en/function.str-split.php>
[2] AFAIK PHP 7 has an efficient implementation of very small strings
(up to 4 bytes?), though.
[3] <https://github.com/nicolas-grekas/Patchwork-UTF8>

-- 
Christoph M. Becker

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


Thread

Functions behaving badly: PHP str_split on an empty string James Harris <james.harris.1@gmail.com> - 2016-02-17 09:57 +0000
  Re: Functions behaving badly: PHP str_split on an empty string "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-02-17 11:53 +0100
    Re: Functions behaving badly: PHP str_split on an empty string James Harris <james.harris.1@gmail.com> - 2016-02-17 16:04 +0000
      Re: Functions behaving badly: PHP str_split on an empty string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-17 23:12 +0100

csiph-web