php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64054 Alternative syntax to access, replace, count a portion of a string or an array
Submitted: 2013-01-23 09:38 UTC Modified: 2013-01-25 07:54 UTC
From: reptilien dot 19831209be1 at gmail dot com Assigned: krakjoe (profile)
Status: Closed Package: Output Control
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: reptilien dot 19831209be1 at gmail dot com
New email:
PHP Version: OS:

 

 [2013-01-23 09:38 UTC] reptilien dot 19831209be1 at gmail dot com
Description:
------------
This is a simply and intuitive proposition to access or replace a part of a 
string or an array. This syntax from partly shell script syntax. We can extend 
this proposition for a simply way to count the number of characteres of a string 
or elements of an array.

Test script:
---------------
$str = "abcdefghi";
$arr = array("j", "k", "l", "m", "n");
$arr1= array("j", "k", array("l", "m"), "n", "o");

Expected result:
----------------
---
Propositions :
---

1. Alternative to substr
$str[0:5] // return 'abcde'
$str[:5] // return 'abcde' (the same as above)
$str[0:-1] // the same as $str
$str[1:2] // return 'bc'
$str[-2] // return 'hi'
$str[-3:1] // return 'g'

2. Alternative to substr_replace
$str[0:5]="jklmn"; // $str is now 'jklmnfghi'
$str[:5]="jklmn"; // $str is now 'jklmnfghi' (the same as above)
$str[-5]="j"; // $str is now 'abcdjfghi'
$str[-5]="jklmn"; // $str is now 'abcdjklmn'
$str[-5]="jklmnopqr"; // $str is now 'abcdjklmn', the rest is ignored

3. Alternative to array_slice()
$arr[0:4] // return 'j', 'k', 'l', 'm'
$arr[:4] // return 'j', 'k', 'l', 'm' (the same as above)
$arr[0:-1] // the same as $arr
$arr[-2] // return 'm', 'n' (if key -2 doesn't exist)
$arr[-2:2] // force to return 'm', 'n' (without ambiguation)
$arr[-3:1] // return 'l'
$arr1[2:2] // return ('l', 'm'), 'n'
$arr1[2:2][0] // return ('l', 'm')
$arr1[2:2][1] // return 'n'

4. Alternative to array_replace()
$arr[0:4]=array('a', 'b', 'c', 'd'); // $arr is now  'a', 'b', 'c', 'd', 'n'
$arr[-2]=array('a', 'b'); // $arr is now  'j', 'k', 'l', 'a', 'b' (if key -2 
doesn't exist)
$arr[-2:2]=array('a', 'b'); // $arr is now  'j', 'k', 'l', 'a', 'b' (forced 
method, without ambiguation)
$arr1[-3:2]=array(array('x', 'y'), 'z'); // $arr1 is now 'j', 'k', ('x', 'y'), 
'z', 'o'
$arr1[-3:2]=array(array('x', 'y'), 'z', 'a'); // $arr1 is now 'j', 'k', ('x', 
'y'), 'z', 'o' (the rest is ignored)

5. Alternative to strlen() and count()
note: in shell script, # is used to count the number of chararacters in a 
string; in php # is used for comments (we need an other symbol)
$str[#] // musn't be used
$str[?] // return 9
$arr[?] // return 5
$arr[1:-1:?] // return 4 (equivalent at count($arr)-1)

Actual result:
--------------
---
Actual results :
---
$str[0]; // return a
$arr[0]; // return j
$arr1[2]; // return ('l', 'm')

$str[0]="x"; // $str is now xbcdefghi
$str[3]="xyz"; // $str is now 'abcxefghi' // the rest 'yz' is ignored
$arr[-1]="x"; // $arr is now 'j', 'k', 'l', 'm', 'n', -1=>'x'

---
Actual errors/warnings :
---
$str[-1] // PHP Warning:  Illegal string offset:  -1 (should return 'i')
$arr[-1] // PHP Notice:  Undefined offset: -1 (should return 'n' if key -1 
doesn't exist)
$arr[-1:1] // PHP Parse error:  syntax error, unexpected ':', expecting ']' 
(should return 'n', none ambiguation)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-24 12:53 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-01-24 12:53 UTC] krakjoe@php.net
I'm going to close the report, because this isn't a bug but a feature request. 
It's also a request likely to be shot down by the development community, still 
all is not lost ...

<?php
class CleverString implements ArrayAccess {
	public $string;
	
	public function __construct($string) {
		$this->string = $string;
	}

	public function offsetSet($key, $value) {

	}
	public function offsetGet($key) {
		if (($break = strpos($key, ":"))) {
			return substr(
				$this->string,
				(int) substr($key, 0, $break),
				(int) substr($key, $break+1)
			);	
		} else return substr($this->string, $key);
	}
	public function offsetExists($key) {

	}
	public function offsetUnset($key) {
	
	}
}

$my = new CleverString("abcdefghi");

printf("\$my[0:5]=%s\n", $my["0:5"]);
printf("\$my[:5]=%s\n", $my["0:5"]);
printf("\$my[0:-1]=%s\n", $my["0:-1"]);
printf("\$my[1:2]=%s\n", $my["1:2"]);
printf("\$my[-2]=%s\n", $my["-2"]);
printf("\$my[-3:1]=%s\n", $my["-3:1"]);
?>

Not a complete implementation, I got bored, but a proof of concept that all you 
want to do is achievable in user land without much effort.
 [2013-01-24 16:27 UTC] reptilien dot 19831209be1 at gmail dot com
Thanks for the reply ;)

It's my first report, I'm not yet familiar with the repport/request system.
So excuse me if I posted in the bad place.
I've chosen "Feature/Change Request" in the "Bug type" section. Wasn't is the 
good way?

About the request/proposition, I believe that it is not more 	
useless than the shorter syntax array recently adopted in version 5.4.

Write Less, Do More...

Thanks for your part of implementation.
 [2013-01-25 07:54 UTC] krakjoe@php.net
You done everything right, I didn't see it was marked as a feature request.

The reason I said it would be shot down is, look at the errors you have there, 
there's one in the parser and a few from execution. If this were a supported 
syntax it could negatively impact the parsing of array manipulation and access 
instructions globally. Write less and do more is right, but it's not always 
appropriate for everything cool to be a core feature or normal practice. 

So write once, and re-use can also be a good way forward :)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Oct 25 05:01:23 2020 UTC