使用 php 获取 intval() ?

发布于 2024-12-04 13:01:25 字数 181 浏览 1 评论 0原文

假设我有以下字符串:

danger13 afno 1 900004

使用 intval() 它给了我 13,但是,我想获取字符串中的最大整数,即 9000004,我怎样才能实现这一点?

编辑:字符串有不同的形式,我不知道最大的数字在哪里。

let's say I have the following string:

danger13 afno 1 900004

using intval() it gives me 13, however, I want to grab the highest integer in the string, which is 9000004, how can I achieve this?

Edit: the string comes in different forms, and I don't know where the highest number will be.

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

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

发布评论

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

评论(4

久伴你 2024-12-11 13:01:25

您需要从字符串中取出所有整数,然后找到最大的...

$str = "danger13 afno 1 900004";
preg_match_all('/\d+/', $str, $matches); // get all the number-only patterns
$numbers = $matches[0];

$numbers = array_map('intval', $numbers); // convert them to integers from string

$max = max($numbers); // get the largest

$max 现在是 900004

请注意,这非常简单。如果您的字符串中有任何与模式 \d+ (1 个或多个数字)匹配的内容,但您不想将其作为单独的整数进行匹配(例如 43.535 将返回 535),这不会让您满意。您需要更仔细地定义您的意思。

You'll need to get all the integers out of the string, then find the biggest...

$str = "danger13 afno 1 900004";
preg_match_all('/\d+/', $str, $matches); // get all the number-only patterns
$numbers = $matches[0];

$numbers = array_map('intval', $numbers); // convert them to integers from string

$max = max($numbers); // get the largest

$max is now 900004.

Note that this is very simple. If your string has anything that matches the pattern \d+ (1 or more digits) that you don't want to match as a separate integer (e.g. 43.535 would return 535), this won't be satisfactory for you. You'll need to define more closely what you mean.

演多会厌 2024-12-11 13:01:25

对于字符串中按字典顺序排列的最高整数值(最多 PHP_INT_MAX),您可以将数字分开并获取最大值:

$max = max(preg_split('/[^\d]+/', $str, NULL, PREG_SPLIT_NO_EMPTY));

演示

或者更好的自我记录:

$digitsList = preg_split('/[^\d]+/', $str, NULL, PREG_SPLIT_NO_EMPTY);
if (!$digitsList)
{
    throw new RuntimeException(sprintf('Unexpected state; string "%s" has no digits.', $str));
}
$max = max($digitsList);

For the lexicographical highest integer value within the string (up to PHP_INT_MAX) you can just split the numbers apart and get the maximum value:

$max = max(preg_split('/[^\d]+/', $str, NULL, PREG_SPLIT_NO_EMPTY));

Demo.

Or better self-documenting:

$digitsList = preg_split('/[^\d]+/', $str, NULL, PREG_SPLIT_NO_EMPTY);
if (!$digitsList)
{
    throw new RuntimeException(sprintf('Unexpected state; string "%s" has no digits.', $str));
}
$max = max($digitsList);
〃安静 2024-12-11 13:01:25
$nums=preg_split('/[^\d\.]+/',$string); //splits on non-numeric characters & allows for decimals
echo max($nums);

ETA:更新为允许以数字结尾或包含数字的“单词”(感谢戈登!)

$nums=preg_split('/[^\d\.]+/',$string); //splits on non-numeric characters & allows for decimals
echo max($nums);

ETA: updated to allow for "words" that end in or contain numbers (Thanks Gordon!)

原来是傀儡 2024-12-11 13:01:25
<?php

$string = 'danger13 afno 1 900004';

preg_match_all('/[\d]+/', $string, $matches);

echo '<pre>'.print_r($matches,1).'</pre>';

$highest = array_shift($matches[0]);

foreach($matches[0] as $v) {
  if ($highest < $v) {
    $highest = $v;
  }
}

echo $highest;

?>
<?php

$string = 'danger13 afno 1 900004';

preg_match_all('/[\d]+/', $string, $matches);

echo '<pre>'.print_r($matches,1).'</pre>';

$highest = array_shift($matches[0]);

foreach($matches[0] as $v) {
  if ($highest < $v) {
    $highest = $v;
  }
}

echo $highest;

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