note 97401 modified in function.substr-replace by danbrown
| From: | danbrown@php.net | Date: | Mon, 19 Apr 2010 14:10:52 +0000 |
| Subject: | note 97401 modified in function.substr-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168413@lists.php.net to get a copy of this message | ||
It's worth noting that when start and length are both negative -and- the length is less than or
equal to start, the length will have the effect of being set as 0.
<?php
substr_replace('eggs','x',-1,-1); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
?>
Same as:
<?php
substr_replace('eggs','x',-1,0); //eggxs
?>
<?php
substr_replace('huevos','x',-2,-2); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
?>
Same as:
<?php
substr_replace('huevos','x',-2,0); //huevxos
?>
Another note, if length is negative and start offsets the same position as length, length (yet
again) will have the effect as being set as 0. (Of course, as mentioned in the manual, when length
is negative it actually represents the position before it)
<?php
substr_replace('abcd', 'x', 0, -4); //xabcd
?>
Same as:
<?php
substr_replace('abcd','x',0,0); //xabcd
?>
<?php
substr_replace('abcd', 'x', 1, -3); //axbcd
?>
Same as:
<?php
substr_replace('abcd', 'x', 1, 0); //axbcd
?>
--was--
It's worth noting that when start and length are both negative -and- the length is less than or
equal to start, the length will have the effect of being set as 0.
substr_replace('eggs','x',-1,-1); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
Same as:
substr_replace('eggs','x',-1,0); //eggxs
substr_replace('huevos','x',-2,-2); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
Same as:
substr_replace('huevos','x',-2,0); //huevxos
Another note, if length is negative and start offsets the same position as length, length (yet
again) will have the effect as being set as 0. (Of course, as mentioned in the manual, when length
is negative it actually represents the position before it)
substr_replace('abcd', 'x', 0, -4); //xabcd
Same as:
substr_replace('abcd','x',0,0); //xabcd
substr_replace('abcd', 'x', 1, -3); //axbcd
Same as:
substr_replace('abcd', 'x', 1, 0); //axbcd
http://php.net/manual/en/function.substr-replace.php