0

我从 http api 收集数据,其中一个 api 是检查我剩余信用的余额。这是我想要做的,获取 file_contents 并从接收到的输出中插入剩余的学分。

        $bal =  file_get_contents("http://www.example.com/balancecheck.php?username=123&api_password=123");

这会输出这样的数据

username = 123
validity = 31/03/2103
remaining credits = 5000
user credits = 3000

现在,我只想获取 5000(这是剩余的积分并将其插入我的数据库)

我想使用爆炸,但没有运气。请有人帮忙

4

1 回答 1

1

来自PHP 文档

描述

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) 在主题中搜索与模式中给出的正则表达式的匹配项。

所以,你需要的是:

preg_match( "/remaining credits = (\d+)/", file_get_contents($url), $matches);
$balance = $matches[1];

编辑:

在函数调用$之前我有一个额外的。file_get_contents我以为 PHP 让你到处都放美元符号。现在应该修好了。

编辑2:

以前有$balance = $matches[0]过,不过已经改正了。括号(\d+)仅捕获数字作为匹配项并将其添加到$matches数组的末尾。

于 2013-03-06T08:50:42.730 回答