The increment of an integer at the end of a line

I have the line "Chicago-Illinos1" and I want to add it to the end, so it will be "Chicago-Illinos2".

Note: it could also be Chicago Illinois10, and I want him to go to Chicago Illinois11, so I can’t make the substrate.

Any suggested solutions?

+3
source share
7 answers

try it

preg_match("/(.*?)(\d+)$/","Chicago-Illinos1",$matches);
$newstring = $matches[1].($matches[2]+1);

(I can’t try, but it should work)

+8
source

Complete solutions for a really simple task ...

$str = 'Chicago-Illinos1';
echo $str++; //Chicago-Illinos2

If the line ends with a number, it will increase the number (for example: 'abc123' ++ = 'abc124').

If the line ends with a letter, the letter will be increased (for example: '123abc' ++ = '123abd')

+11
source
$string = 'Chicago-Illinois1';
preg_match('/^([^\d]+)([\d]*?)$/', $string, $match);
$string = $match[1];
$number = $match[2] + 1;

$string .= $number;

, .

+2

,

<?php
$str="Chicago-Illinos1"; //our original string

$temp=explode("Chicago-Illinos",$str); //making an array of it
$str="Chicago-Illinos".($temp[1]+1); //the text and the number+1
?>
+1

, ( Java [0-9] + $), (int number = Integer.parse(yourNumberAsString) + 1), - ( , ).

0

preg_match :

$name = 'Chicago-Illinos10';
preg_match('/(.*?)(\d+)$/', $name, $match);
$base = $match[1];
$num = $match[2]+1;
print $base.$num;

:

Chicago-Illinos11

, , . , , explode . .

$name = 'Chicago-Illinos|1';
$parts = explode('|', $name);
print $parts[0].($parts[1]+1);

(, ), . (.. Chicago-IL|1)

0
source
$str = 'Chicago-Illinos1';
echo ++$str;

http://php.net/manual/en/language.operators.increment.php

0
source

Source: https://habr.com/ru/post/1786505/


All Articles