0

I faced with some issue to increase the vale of the string, for example:

$string = "someString_1";

I need value somestring_2. Ofcourse I can use something like this. But was trying to find another way and as a result I found that it will be enought make $string++ in my case;

$string++;
echo $string; //someString_2

It was great that I have only 1 string of code. BUT: Queston, is this correct way, or is there more better variants?(in condition that my string allways will have integer at the end)

And why it does not work when integer at the first position. For example if we make:

echo '1string' + '2string';// result 3. This is how PHP work with string when we want make some mathemetic operations

5
  • This will have some problems when you have $string = "someString_9"; $string++; echo $string; Commented Jan 26, 2014 at 13:18
  • stackoverflow.com/questions/8885440/… -- incrementing numeric strings works, but is not proper code. RegEx is the better solution Commented Jan 26, 2014 at 13:19
  • @Mark Baker. Eh , I just thing about it. I should set 09 it that case to get 10, not good idea( Commented Jan 26, 2014 at 13:20
  • But if you set someString_09, what will happen when you reach someString_99.... string incrementing is extremely useful in the right circumstances, but does have its limitations Commented Jan 26, 2014 at 13:22
  • Agree totaly, in project range will be 0-9, but who knows what will be later.. Commented Jan 26, 2014 at 13:24

3 Answers 3

2

you can try this regular expression. This snippet works and it gave me result as someString_2

 $string = "someString_1";
 function increment($matches) {
   return ++$matches[1];
 }

echo $string =  preg_replace_callback( "|(\d+)|", "increment", $string);
Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps this response could be improved by using an anonymous function. I don't like to create a function and then call it from one place only (because later maybe I remove the preg_replace_callback() and forget the function and then somebody else wonders why the increment function exists).
2

There is an explanation on php.net http://www.php.net/manual/en/language.operators.increment.php

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

Comments

1

Your solution probably works, but imagine, that someone has to modify your code some time later. Would you guess (without any research) what it does, if you saw $string++ somewhere? Even if it works, it's much better in my opinion, to create a function which does what you need, and has a name, which makes it clear, what it does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.