相关疑难解决方法(0)

从字符串中提取数字

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

"In My Cart : 11 items"
Run Code Online (Sandbox Code Playgroud)

我想在这里得到号码11或任何其他号码.

php

300
推荐指数
13
解决办法
44万
查看次数

使用PHP从字符串中获取数字

我有字符串:

$one = 'foo bar 4 baz (5 qux quux)';
$two = 'bar baz 2 bar';
$three =  'qux bar 12 quux (3 foo)';
$four = 'foo baz 3 bar (13 quux foo)';
Run Code Online (Sandbox Code Playgroud)

如何在这些字符串中找到数字?

也许有功能:

function numbers($string){

    // ???

    $first = ?;
    $second = ?;
}
Run Code Online (Sandbox Code Playgroud)

例如:

function numbers($one){

    // ???

    $first = 4;
    $second = 5;
}

function numbers($two){

    // ???

    $first = 2;
    $second = NULL;
}
Run Code Online (Sandbox Code Playgroud)

最好的方法可能是正则表达式,但我怎么能用这个作为我的例子呢?也许没有正则表达式?

php regex

9
推荐指数
3
解决办法
6万
查看次数

标签 统计

php ×2

regex ×1