php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #15329 Subscripting into strings would be useful
Submitted: 2002-02-01 13:48 UTC Modified: 2002-02-04 10:16 UTC
From: hek at loudoun-net dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.1.1 OS: N/A
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hek at loudoun-net dot com
New email:
PHP Version: OS:

 

 [2002-02-01 13:48 UTC] hek at loudoun-net dot com
The now very old North Star BASIC had a wonderful feature that I'd love to see in PHP, which was the ability to access strings using subscripts, rather like subscripting into an array, except even better since it allowed ranges to be subscripted just as easily as single characters in the strings.

Examples:

$string = "My String";

echo $string[5]; // returns "r"
echo $string[0,]; // returns "My String"
echo $string[3,5]; // returns "Str"
echo $string[,3]; // returns "My S"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-01 14:50 UTC] derick@php.net
Already supported by {} and substr.

Derick
 [2002-02-04 10:16 UTC] hek at loudoun-net dot com
Thank you.  I know about substr() -- which is rather cumbersome compared with the notation I suggested -- but I don't know what you mean by {}.

I cannot find this notation explainined in the PHP Manual (at least not in the pages about array functions, about the array type, nor in the pages about operators).

Where is {} usage described?

--HaigEK
 [2002-02-04 11:04 UTC] hek at loudoun-net dot com
Ooops.  Getting confused about my posts (arrays versus strings).

OK, I've found the very brief comments on "String access by character" -- that is, using {} to reference characters within strings -- documented near the end of http://www.php.net/manual/en/language.types.string.php in the PHP Manual.

As described this gets the caller a single charecter.  A nice enhancement would be to let the syntax also support ranges, by accomodating an optional second argument, length.  Examples:

$string = "abcde";
echo $string{1,3}; // To get "bcd" -- offset 1, length 3

But then you'd have to think about how to handle negative values, blah, blah, blah.

I know, I know...  You're going to bogus this because it's not likely to ever get implemented.  Never mind.

You might think about listing the {} operators (and the [], ->, and & operators, too) in the operators section of the manual, however, to make it easier for people to find out about them in the future.

--HaigEK
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 22:01:29 2024 UTC