From: reptilien dot 19831209be1 at gmail dot com Operating system: PHP version: Irrelevant Package: Output Control Bug Type: Feature/Change Request Bug description:Alternative syntax to access, replace, count a portion of a string or an array
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 bug report at https://bugs.php.net/bug.php?id=64054&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64054&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64054&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64054&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=64054&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=64054&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=64054&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=64054&r=needscript Try newer version: https://bugs.php.net/fix.php?id=64054&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=64054&r=support Expected behavior: https://bugs.php.net/fix.php?id=64054&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=64054&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=64054&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=64054&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64054&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=64054&r=dst IIS Stability: https://bugs.php.net/fix.php?id=64054&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=64054&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=64054&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=64054&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=64054&r=mysqlcfg