在字符串末尾增加整数

发布于 2024-10-13 07:57:48 字数 210 浏览 4 评论 0原文

我有一个字符串 Chicago-Illinos1,我想在其末尾添加一个,因此它是 Chicago-Illinos2

注意:它也可能是 Chicago-Illinos10 并且我希望它转到 Chicago-Illinos11 所以我不能依赖 substr()

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().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

忆依然 2024-10-20 07:57:48

一个非常简单的问题的复杂解决方案...

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

如果字符串以数字结尾,它将递增数字(例如:'abc123'++ = 'abc124')。

如果字符串以字母结尾,则该字母将递增(例如:'123abc'++ = '123abd')

Complex solutions for a really simple problem...

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

If the string ends with a number, it will increment the number (eg: 'abc123'++ = 'abc124').

If the string ends with a letter, the letter will be incremeted (eg: '123abc'++ = '123abd')

小…红帽 2024-10-20 07:57:48

试试这个

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

(现在不能尝试,但应该可以)

Try this

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

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

债姬 2024-10-20 07:57:48
$string = 'Chicago-Illinois1';
preg_match('/^([^\d]+)([\d]*?)$/', $string, $match);
$string = $match[1];
$number = $match[2] + 1;

$string .= $number;

经过测试,有效。

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

$string .= $number;

Tested, works.

风流物 2024-10-20 07:57:48

爆炸也可以完成这项工作

<?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
?>

explode could do the job aswell

<?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
?>
北座城市 2024-10-20 07:57:48

我将使用正则表达式来获取字符串末尾的数字(对于Java,它是[0-9]+$),增加它(int number = Integer.parse (yourNumberAsString) + 1),并与芝加哥伊利诺斯州连接(其余部分与用于查找号码的正则表达式不匹配)。

I would use a regular expression to get the number at the end of a string (for Java it would be [0-9]+$), increase it (int number = Integer.parse(yourNumberAsString) + 1), and concatenate with Chicago-Illinos (the rest not matched by the regular expression used for finding the number).

貪欢 2024-10-20 07:57:48

您可以使用 preg_match 来完成此操作:

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

将输出以下内容:

Chicago-Illinos11

但是,如果可能的话,我建议在文本和数字之间放置另一个分隔字符。例如,如果您放置了一个管道,您可以简单地执行爆炸并获取数组的第二部分。那就简单多了。

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

如果字符串长度是一个问题(因此伊利诺伊州的拼写错误),您可以切换到州缩写。 (即芝加哥-IL|1

You can use preg_match to accomplish this:

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

The following will output:

Chicago-Illinos11

However, if it's possible, I'd suggest placing another delimiting character between the text and number. For example, if you placed a pipe, you could simply do an explode and grab the second part of the array. It would be much simpler.

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

If string length is a concern (thus the misspelling of Illinois), you could switch to the state abbreviations. (i.e. Chicago-IL|1)

×纯※雪 2024-10-20 07:57:48

在不预先隔离数字字符串后缀的情况下,有两种选项可以增加它。

  1. ++$string$string++ - 一元运算符允许值的递增和递减

  2. 从 PHP8.3 开始,str_increment()可以用稍微更稳定的行为来调用,但字符串本身必须完全是字母数字。请注意,如果您需要递增字符串数组,则使用此新的本机函数允许函数迭代器(例如 array_map())仅通过名称调用 str_increment。 p>

现在坏消息...

如果整个后缀数字通常会获得另一个数字,这两种技术都会破坏左相邻字符。例如,a9 将增加到 b0z99 将变为 aa00

如果这还不足以让您担心,那么还存在增加类似于浮点值的字符串值的风险。字符串值 4567e4 将递增到浮点类型值 45670001.0

阅读手册:https://www.php.net/manual/ en/language.operators.increment.php


因为那些“方便”的技术相当不可靠,所以更好的通用方法是分离、递增和替换数字后缀。

您可以通过调用 preg_replace_callback() 来避免声明临时变量。我推荐这种技术的直接性和稳定性——它总是返回一个字符串。匹配整个数字后缀,递增它,然后用递增后的后缀替换原始后缀。无副作用。

代码:(演示)

$string = 'Chicago41Illinos9';

var_export(
    preg_replace_callback(
        '/\d+$/',
        fn($m) => ++$m[0],
        $string
    )
);
// Chicago41Illinos10

要编辑字符串中任意位置的数字,只需删除 $(字符串锚点的结尾)来自模式。

Without pre-isolating the numeric string suffix, there are two options to increment it.

  1. ++$string or $string++ - unary operators allow the incrementation and decrementation of values

  2. As of PHP8.3, str_increment() can be called with slightly more stable behavior, but the string itself must be entirely alphanumeric. Note that if you needed to increment an array of strings, having this new native function permits functional iterators (like array_map()) to call str_increment by its name alone.

Now for the bad news...

Both of these techniques will corrupt the left-neighboring character if the whole suffix number would ordinarily gain another digit. For instance, a9 would increment to b0 and z99 would become aa00.

If that wasn't enough to worry you, there is also the risk of incrementing a string value that resembles a float value. The string value 4567e4 would be incremented to the float-type value 45670001.0.

Read the manual: https://www.php.net/manual/en/language.operators.increment.php


Because those "convenient" techniques are rather unreliable, a better general-use approach would be to separate, increment, and replace the numeric suffix.

You can avoid declaring a temporary variable by calling preg_replace_callback(). I recommend this technique for directness and stability -- it will always return a string. Match the whole numeric suffix, increment it, then replace the original suffix with the incremented suffix. No side effects.

Code: (Demo)

$string = 'Chicago41Illinos9';

var_export(
    preg_replace_callback(
        '/\d+$/',
        fn($m) => ++$m[0],
        $string
    )
);
// Chicago41Illinos10

To edit a number anywhere in the string, just remove the $ (end of string anchor) from the pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文