Edit report at https://bugs.php.net/bug.php?id=64054&edit=1

 ID:                 64054
 Updated by:         krak...@php.net
 Reported by:        reptilien dot 19831209be1 at gmail dot com
 Summary:            Alternative syntax to access, replace, count a
                     portion of a string or an array
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Output Control
 PHP Version:        Irrelevant
-Assigned To:        
+Assigned To:        krakjoe
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-01-23 09:38:35] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64054&edit=1

Reply via email to