php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43950 Char-wise v byte-wise string indexing
Submitted: 2008-01-27 23:17 UTC Modified: 2017-03-13 13:10 UTC
From: jrf_temp_2003 at comcast dot net Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-01-27 23:17 UTC] jrf_temp_2003 at comcast dot net
Description:
------------
When I "upgraded" to multibyte char encoding and functions, I discovered that I could no longer rely on $s{strlen($s)-1} to access the last character in a string. Strlen correctly counted only whole chars, but the indexing came up short by the number of double-wide chars in the string and hit the wrong char. I could (and did) patch my scripts with rtrim, but others may not be so lucky. I had an idea...

Suggestion:
Some time ago, square brackets were deprecated in favor of curly braces for individual char access in a string. Could both be employed, each for a different purpose? Could curly braces to be used for multibyte-sensitive indexing? Could square brackets, as always, offer simple byte-wise access?

Reproduce code:
---------------
// After a loop chains field names + commas...

$sql{strlen($sql)-1} = ' '; // Replace last trailing comma with a space
$sql.= $where_clause;

// Because my aim was at the very end of a string, rtrim could solve
// my problem. Less specialized cases would not be so easy.


Expected result:
----------------
// There should be two chars in quotes and no comma between the quoted
// value and WHERE

... x="**" WHERE ...


Actual result:
--------------
// Two mb chars elsewhere in string displaced the substitution
// by two places to the left, wiping out a quoted char and leaving
// the comma:

... x="* ",WHERE ...

// Could it be possible for $s{} to index by mb chars? If so, then
// $s[] could be resurrected to index by bytes as both do currently.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-13 13:10 UTC] narf@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2017-03-13 13:10 UTC] narf@php.net
mbstring.func_overload is deprecated in PHP 7.2 and will be removed in 8.0.

https://wiki.php.net/rfc/deprecations_php_7_2#mbstringfunc_overload
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 08:01:30 2024 UTC