从字符串中获取数字

Kic*_*ano -4 php regex

我有以下字符串:

Arsenal (0) - Swansea City (3)
Run Code Online (Sandbox Code Playgroud)

第一步我想得到数字(在这种情况下为0和3)第二步从字符串中删除(数字).

Ry-*_*Ry- 7

你可以用preg_replace_callback.

$myString = "Arsenal (0) - Swansea City (3)";
$newString = preg_replace_callback('/\d+/', function($match) {
    // The number matched is stored in $match, do whatever you need with it
    return ''; // Replace with nothing
}, $myString);
// $newString now contains "Arsenal () - Swansea City ()".
Run Code Online (Sandbox Code Playgroud)

如果你想剥离()(和空格),修改正则表达式和你的回调代码:

'/\s*\(\d+\)/'
Run Code Online (Sandbox Code Playgroud)