php 从字符串中提取数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6278296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:53:19  来源:igfitidea点击:

Extract numbers from a string

php

提问by Bizboss

I want to extract the numbers from a string that contains numbers and letters like:

我想从包含数字和字母的字符串中提取数字,例如:

"In My Cart : 11 items"

I want here to get the number 11or any other number.

我想在这里获取号码11或任何其他号码。

回答by Daniel B?ndergaard

If you just want to filter everything other than the numbers out, the easiest is to use filter_var:

如果您只想过滤除数字以外的所有内容,最简单的方法是使用filter_var

$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);

回答by Gaurav

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

回答by Mahipalsinh Ravalji

preg_replace('/[^0-9]/', '', $string);

This should do better job!...

这应该做得更好!...

回答by Akash Deep

Using preg_replace:

使用preg_replace

$str = '(111) 111-1111';
$str = preg_replace('/\D/', '', $str);
echo $str;

Output: 1111111111

输出:1111111111

回答by Yash

For floating numbers,

对于浮点数,

preg_match_all('!\d+\.?\d+!', $string ,$match);

Thanks for pointing out the mistake. @mickmackusa

感谢您指出错误。@mickmackusa

回答by mroncetwice

I do not own the credit for this, but I just have to share it. This regex will get numbers from a string, including decimal points/places, as well as commas:

我不拥有这方面的功劳,但我只需要分享它。此正则表达式将从字符串中获取数字,包括小数点/位,以及逗号:

/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/

/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/

Cited from here:
php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

从这里引用:
php - regex - 如何从字符串(例如 1,120.01)中提取带十进制(点和逗号)的数字?

回答by Hariadi

Using preg_replace

使用preg_replace

$str = 'In My Cart : 11 12 items';
$str = preg_replace('/\D/', '', $str);
echo $str;

回答by Dmitri Gudkov

You can use preg_match:

您可以使用preg_match

$s = "In My Cart : 11 items";
preg_match("|\d+|", $s, $m);
var_dump($m);

回答by Jasmeen

You can use following function:

您可以使用以下功能:

function extract_numbers($string)
{
   preg_match_all('/([\d]+)/', $string, $match);

   return $match[0];
}

回答by Bradley Kovacs

preg_match_all('!\d+!', $some_string, $matches);
$string_of_numbers = implode(' ', $matches[0]);

The first argument in implode in this specific case says "separate each element in matches[0] with a single space." Implode will not put a space (or whatever your first argument is) before the first number or after the last number.

在这种特定情况下,implode 中的第一个参数表示“将matches[0] 中的每个元素用一个空格分隔”。Implode 不会在第一个数字之前或最后一个数字之后放置一个空格(或任何您的第一个参数)。

Something else to note is $matches[0] is where the array of matches (that match this regular expression) found are stored.

需要注意的另一件事是 $matches[0] 是存储找到的匹配数组(与此正则表达式匹配)的位置。

For further clarification on what the other indexes in the array are for see: http://php.net/manual/en/function.preg-match-all.php

有关数组中其他索引的进一步说明,请参见:http: //php.net/manual/en/function.preg-match-all.php