我想从包含数字和字母的字符串中提取数字:
"In My Cart : 11 items"
我想提取数字 11
。
我想从包含数字和字母的字符串中提取数字:
"In My Cart : 11 items"
我想提取数字 11
。
由于您的字符串中只有一个数字值需要隔离,我将推荐并个人使用filter_var()
与FILTER_SANITIZE_NUMBER_INT
。
echo filter_var($string, FILTER_SANITIZE_NUMBER_INT);
ltrim()
函数与字符掩码,将剩余的字符串转换成整数,因为在这种情况下只有1个数值和该整数之前的唯一字符都是字母、冒号或空格。
$string = "In My Cart : 11 items";
echo (int)ltrim($string, 'A..z: ');
// 11
如果由于某种原因出现了多个整数值并且您想要获取第一个,则正则表达式将是一种直接的技术。
echo preg_match('/\d+/', $string, $m) ? $m[0] : '';
sscanf()
十分方便,如果您需要将数字字符串显式转换为整数(或浮点数)。 如果整数值可能/未知出现在字符串的开头,则在扫描输入字符串之前在其前面添加一个非数字字符。以下技术匹配前导非数字字符(并使用%
后面的*
忽略它们),然后匹配第一次出现的数字序列,并将返回的子字符串转换为整数。
var_dump(sscanf(' ' . $string, '%*[^0-9]%d')[0]);
d
为 f
。有关sscanf()
(目前未记录的)赋值抑制功能的更多信息,请参见此帖子。function extract_numbers($string)
{
preg_match_all('/([\d]+)/', $string, $match);
return $match[0];
}
preg_match_all('!\d+!', $some_string, $matches);
$string_of_numbers = implode(' ', $matches[0]);
preg_match_all()
是不必要的额外工作,并且它创建了一个具有不必要深度的 $matches
数组。这看起来像是那种完全忘记了 OP 在问题中所问的内容的问题。 - mickmackusa尝试使用preg_replace函数进行操作。
$string = "Hello! 123 test this? 456. done? 100%";
$int = intval(preg_replace('/[^0-9]+/', '', $string), 10);
echo $int;
123456100
有什么可能的用途? - mickmackusa$string = 'In My Car_Price : 50660.00';
echo intval(preg_replace('/[^0-9.]/','',$string)); # without number format output: 50660
echo number_format(intval(preg_replace('/[^0-9.]/','',$string))); # with number format output :50,660
示例: http://sandbox.onlinephpfunctions.com/code/82d58b5983e85a0022a99882c7d0de90825aa398
按照以下步骤操作,即可将字符串转换为数字
$value = '$0025.123';
$onlyNumeric = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
settype($onlyNumeric,"float");
$result=($onlyNumeric+100);
echo $result;
另外一种方法:
$res = preg_replace("/[^0-9.]/", "", "$15645623.095605659");
使用sscanf的另一种解决方案:
$str = "In My Cart : 11 items";
list($count) = sscanf($str, 'In My Cart : %s items');
%d
也可以用于将数字子字符串转换为整数类型数据。https://3v4l.org/lLTXv @salathe在2011年提到了这个技巧,但被OP说服删除答案,理由是完整的字符串没有可靠的静态格式。(文本的可变性应该由OP澄清。) - mickmackusa另一种方式(甚至是Unicode字符串):
$res = array();
$str = 'test 1234 555 2.7 string ..... 2.2 3.3';
$str = preg_replace("/[^0-9\.]/", " ", $str);
$str = trim(preg_replace('/\s+/u', ' ', $str));
$arr = explode(' ', $str);
for ($i = 0; $i < count($arr); $i++) {
if (is_numeric($arr[$i])) {
$res[] = $arr[$i];
}
}
print_r($res); //Array ( [0] => 1234 [1] => 555 [2] => 2.7 [3] => 2.2 [4] => 3.3 )
preg_split()
,而要用这么复杂的方法。 - undefined$string = '$125.22';
$string2 = '$125';
preg_match_all('/(\d+.?\d+)/',$string,$matches); // $matches[1] = 125.22
preg_match_all('/(\d+.?\d+)/',$string2,$matches); // $matches[1] = 125
ltrim($string,'$')
。 - mickmackusa对于UTF8字符串:
function unicodeStrDigits($str) {
$arr = array();
$sub = '';
for ($i = 0; $i < strlen($str); $i++) {
if (is_numeric($str[$i])) {
$sub .= $str[$i];
continue;
} else {
if ($sub) {
array_push($arr, $sub);
$sub = '';
}
}
}
if ($sub) {
array_push($arr, $sub);
}
return $arr;
}
utf8
准备好的,因为你没有使用mb_
函数,而且你使用[offset]
语法来访问字符串中的每个字节。这个未解释的答案是“不好的”。 - mickmackusa
\d
没有任何好处。由于您正在访问完整的字符串匹配,因此在模式中编写捕获组是没有意义的。 - mickmackusapreg_match_all()
不是解决所提问题的合适工具。这个答案最多只能算是对另一个问题的回答。 - mickmackusa