0

How to increase a complete string in php. Like abc should be converted to bcd. I tried

$i="abc";
$i++;
echo $i; //abd

But it increases only the last character.

I searched many question like this but I did not find any solutions.

0stone0
  • 34,288
  • 4
  • 39
  • 64
Azad
  • 11
  • 1
  • 3
    so if it works for one character, maybe try a **loop** and increment every character? – Adrian Kokot Sep 14 '21 at 17:42
  • 1
    What would you expect xyz to be converted to? z by default will increment to aa, but if you want it to rotate to a instead you'll need to do something different. – Don't Panic Sep 14 '21 at 17:54
  • Sir how to increment a string by two or three eg i want to conert hi to "jk – Azad Sep 14 '21 at 18:16
  • @Azad What is your actual question? You seem to be posting a different question and looking for a different answer? – nice_dev Sep 14 '21 at 18:48

2 Answers2

1

You probably have read How to increment letters like numbers in PHP?.

However, the $i++ only works on a single char.


So to get the desired output, we could simply:

  1. Convert the string into an array using str_split()
  2. Loop over them using a foreach() , change, or in my case create a new output
    1. 'Increase' the char using ++
<?php

$test = 'abc';
$output = '';

foreach (str_split($test) as $char) {
    $output .= ++$char;
}

echo $output;

The above code will produce:

bcd

as you can try in this online demo.


A more compact solution would involve

<?php

$test = 'abc';
$result = implode(array_map(fn ($c) => ++$c, str_split($test)));

echo $result;

Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
1

We can do this by the following 3 steps:

  1. Convert each character to an unsigned integer, by using php's built-in function ord().
  2. Increment this integer by one (or by other number).
  3. Convert it back to character (using php's built-in functon chr()).
$test = 'abc';
$output = '';

foreach (str_split($test) as $char) {
    $output .= chr(ord($char)+1);
}

echo $output;

See demo

Jurakin
  • 832
  • 1
  • 5
  • 19
  • Sir how to increment a string by two or three eg i want to conert hi to "jk" – Azad Sep 14 '21 at 18:15
  • 2
    @Azad: Make an effort, you have the solution in front of you! – Casimir et Hippolyte Sep 14 '21 at 18:38
  • 1
    Just replace number 1 in `$output .= chr(ord($char)+1);` with another number eg 2. – Jurakin Sep 14 '21 at 18:40
  • 1
    "Try this" does not make for a good answer. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](//stackoverflow.com/help/how-to-answer). This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – John Conde Sep 14 '21 at 20:26
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 15 '21 at 01:13