PHP:获取给定格式化字符串末尾的数值

发布于 2024-12-04 20:15:00 字数 864 浏览 4 评论 0原文

我“继承”了一个有缺陷的 PHP 页面。我不是这门语言的专家,但我想我找到了错误的根源。在一个循环中,页面向服务器发送一个格式化的字符串:我在 HTML 页面中找到的字符串如下所示:

2011-09-19__full_1

所以,看起来我们有三个部分:

  • 日期 (0,10);日期 (0,10);
  • 字符串 (10,6);
  • 最终数字 (17,1);

处理这种情况的代码如下:

$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);

发生了什么:最终数字可以包含多个字符,因此这一段:

"class_id"=>substr($post_array_keys[$i], 17, 1)

不正确,因为它似乎只检索从第 17 个字符开始的一个字符(这似乎会导致网站出现奇怪的行为)。

作为字符串的最后一部分的整数,要获取整个数字我可以这样安全地更改此行吗?

"class_id"=>substr($post_array_keys[$i], 17, strlen($post_array_keys[$i])-17);

I "inherited" a buggy PHP page. I'm not an expert of this language but I think I found the origin of the bug. Inside a loop, the page sends a formatted string to the server: the string I found in the HTML page is like this one:

2011-09-19__full_1

so, it seems we have three parts:

  • a date (0,10);
  • a string (10,6);
  • a final number (17,1);

The code the handles this situation is the following:

$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);

What happens: the final number can contain more than one character, so this piece:

"class_id"=>substr($post_array_keys[$i], 17, 1)

is not correct because it seems to retrieve only one character starting from the 17th (and this seems to cause strange behaviors to the website).

Being the whole number the last part of the string, to get the entire number could I safely change this line this way?

"class_id"=>substr($post_array_keys[$i], 17, strlen($post_array_keys[$i])-17);

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

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

发布评论

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

评论(3

热风软妹 2024-12-11 20:15:00

如果按照您建议的方式更改代码,您将获得从位置 17 开始的末尾数字。原始代码仅获得第一位数字。您的代码将获得所有数字。

看来你做了功课,这一行

$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);

确实给了你一个很好的线索,告诉你你应该在变量中期待什么:

first 10 is the date
then you have 6 chars for post_mode
then you have 1 char for class_id

如果你还确认有时 class_id 可以超过 1 个字符,你建议的更改将为你提供完整的 class_id结束。

祝你好运。

If you change the code the way you suggest you would get the numbers at the end starting in position 17. The original code gets only the first digit. Your code would get all the digits.

And it seems you did your homework the line

$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);

does give you a very good clue of what you should expect in the variable:

first 10 is the date
then you have 6 chars for post_mode
then you have 1 char for class_id

If you also confirmed that sometimes the class_id can be more than 1 char, your suggested change would give you the complete class_id at the end.

Good luck.

篱下浅笙歌 2024-12-11 20:15:00

您可以使用

$array =  explode("_", $string);

此函数返回一个数组,其中字符串中的元素由“_”分隔。

我建议这样做是因为双下划线可能会隐藏在该特定情况下为空的另一个值。

you could use

$array =  explode("_", $string);

this functions returns an array with the elements in the string delimited by "_".

I suggest this because the double underscore may hide another value that is empty in that particular case.

枫以 2024-12-11 20:15:00

如果只是最后一个整数造成问题,您可以使用 strrchr 获取字符串的“尾部”,从最后一个“_”开始。

If it's only the last integer causing trouble, you can use strrchr to get the "tail" of the string, starting with the last '_'.

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