Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16460
| From | James Harris <james.harris.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Functions behaving badly: PHP str_split on an empty string |
| Date | 2016-02-17 16:04 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <na25ho$q84$1@dont-email.me> (permalink) |
| References | <na1g0b$va5$1@dont-email.me> <na1jeb$8hm$1@solani.org> |
On 17/02/2016 10:53, Christoph M. Becker wrote:
> James Harris wrote:
...
>> 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).
That was useful in making it possible for me to check the types of
strings. Given:
$request_uri = $_SERVER["REQUEST_URI"];
$uri = urldecode($request_uri)
On this server (Apache) $request_uri is ASCII, even if the URL had some
Chinese characters in it.
As for $uri, if the input is just ASCII characters then $uri (the output
of urldecode) is plain ASCII. But if the input contains Chinese
characters then $uri will be Unicode.
I presume my choices are essentially either to report non-ASCII
characters as errors or to do all processing in Unicode.
> 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).
There may be a better way to do it. Here is some of my current code
$request_uri = trim(urldecode($_SERVER["REQUEST_URI"]), "/");
foreach (str_split($request_uri) as $char) {
if (strpos($path_chars_permitted, $char) === false) {
$etext .= " Invalid character in URL: \"" .
htmlspecialchars($char) . "\"<br/>\n";
}
}
Suggestions for improvement welcome. Maybe there is a function to do the
lot in one go that would save me iterating over the string. I think the
best thing would be to convert the above to handle Unicode. That would
allow better error messages.
(I now know that str_split won't work when $request_uri is empty.)
James
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