Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16442
| Path | csiph.com!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
| Newsgroups | comp.lang.php |
| Subject | Re: Functions behaving badly: PHP str_split on an empty string |
| Date | Wed, 17 Feb 2016 11:53:02 +0100 |
| Organization | solani.org |
| Lines | 51 |
| Message-ID | <na1jeb$8hm$1@solani.org> (permalink) |
| References | <na1g0b$va5$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | solani.org 1455706379 8758 eJwNxsEBwCAIA8CVJJhYxwEK+4/Q3uvoMtXZojaH04+HFPVm99AZF3HNVg3/DCIhOKlCrjz2AS+9EU4= (17 Feb 2016 10:52:59 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Wed, 17 Feb 2016 10:52:59 +0000 (UTC) |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 |
| X-User-ID | eJwFwQERADEIAzBLA9r+IYcf4F/CEoZM94MocLmo0si7nWN/zF4bB5o31zJPRLqX1UqDk/0AJ8ERLA== |
| In-Reply-To | <na1g0b$va5$1@dont-email.me> |
| Cancel-Lock | sha1:LZrAOW29BvmJAkVELxvEtxMWnow= |
| X-NNTP-Posting-Host | eJwFwYEBwDAEBMCVUF6NI+L3HyF38UEx6Qh4MLhnu4XUg9W9UyQtR9DwjU3rufZHaZrXlDxM6hH3 |
| Xref | csiph.com comp.lang.php:16442 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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