使用正则表达式从文本中提取数字

发布于 2024-09-16 21:21:04 字数 170 浏览 4 评论 0原文

我正在尝试使用 PHP 从此文本中提取 1125

preg_match("/^(?P<digit>\d+)/", "1 Foo ($125)",$m)

您能帮忙吗?

谢谢

I am trying to extract 1 and 125 from this text with PHP:

preg_match("/^(?P<digit>\d+)/", "1 Foo ($125)",$m)

Can you please help?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓味的萝莉 2024-09-23 21:21:04

试试这个:

<?php
preg_match_all('/\d+/', '1 Foo ($125) bar', $matches);
var_dump($matches);
?>

请注意,我使用了 preg_match_all 它实际上返回模式中的所有匹配项。

Try this:

<?php
preg_match_all('/\d+/', '1 Foo ($125) bar', $matches);
var_dump($matches);
?>

Note that I used preg_match_all which literally returns all matches in the pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文