There is an inconsistency in increment/decrement operator function regarding strings in -at least- my version of PHP. Here is what I mean:
php > echo phpversion(); 7.4.11 php > $test = 'abc12'; php > // increment works as expected php > echo(++$test); abc13 php > echo(++$test); abc14 php > echo(++$test); abc15 php > // but decrement fails php > echo(--$test); abc15 php > echo(--$test); abc15 php > echo(--$test); abc15
Is this an expected behavior? Should I file a bug report or something? Do you know of a workaround?
edit: filed bug#80212
Advertisement
Answer
Here is what I finally went with:
function decrement($str) {
$matches = preg_split('/(d+)z/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
@--$matches[1];
return implode($matches);
}
Wouldn’t call it elegant but I’d call it functional.