phpstringincrement

Increment integer at end of string


I have a string, Chicago-Illinos1 and I want to add one to the end of it, so it would be Chicago-Illinos2.

Note: It could also be Chicago-Illinos10 and I want it to go to Chicago-Illinos11 so I can't rely on substr().


Solution

  • Try this

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

    (can't try it now but it should work)