How to increment numeric part of a string by one?

前端 未结 5 1593
耶瑟儿~
耶瑟儿~ 2020-12-11 01:39

I have a string formed up by numbers and sometimes by letters.

Example AF-1234 or 345ww.

I have to get the numeric part and incre

5条回答
  •  不思量自难忘°
    2020-12-11 01:41

    If the string ends with numeric characters it is this simple...

    $str = 'AF-1234';
    echo $str++; //AF-1235
    

    That works the same way with '345ww' though the result may not be what you expect.

    $str = '345ww';
    echo $str++; //345wx
    

    @tampe125

    This example is probably the best method for your needs if incrementing string that end with numbers.

    $str = 'XXX-342';
    echo $str++; //XXX-343
    

提交回复
热议问题