我有一个字符串,"Chicago-Illinos1",我想在它的末尾添加一个,所以它将是"Chicago-Illinos2".
注意:它也可能是Chicago-Illinos10,我希望它去Chicago-Illinos11所以我不能做substr.
有建议的解决方案
Mav*_*elo 11
解决一个非常简单的问题的复杂解
$str = 'Chicago-Illinos1';
echo $str++; //Chicago-Illinos2
Run Code Online (Sandbox Code Playgroud)
如果字符串以数字结尾,则会增加数字(例如:'abc123'++ ='abc124').
如果字符串以字母结尾,则字母将被递增(例如:'123abc' ++ ='123abd')
试试这个
preg_match("/(.*?)(\d+)$/","Chicago-Illinos1",$matches);
$newstring = $matches[1].($matches[2]+1);
Run Code Online (Sandbox Code Playgroud)
(现在不能尝试,但它应该工作)