使用PHP捕获正则表达式的字符串的一部分?

时间:2022-12-01 09:57:24

I can't seem to find a function that captures and returns a regex'ed string.

我似乎无法找到捕获并返回正则表达式字符串的函数。

$string = "hello123!";
$string = regexfunction($string, "/([0-9]*)/");
echo $string;

Prints

123

1 个解决方案

#1


You want to use preg_match for this, although the syntax will be slightly different:

你想为此使用preg_match,虽然语法会略有不同:

$string = "hello123!";
preg_match("/[0-9]+/", $string, $matches);
print $matches[0]; // 123

#1


You want to use preg_match for this, although the syntax will be slightly different:

你想为此使用preg_match,虽然语法会略有不同:

$string = "hello123!";
preg_match("/[0-9]+/", $string, $matches);
print $matches[0]; // 123