有人能将我的小 Python 代码片段翻译成 PHP 吗?我对这两种语言都不太熟悉 :(
matches = re.compile("\"cap\":\"(.*?)\"")
totalrewards = re.findall(matches, contents)
print totalrewards
感谢那些提供帮助的人!:(
有人能将我的小 Python 代码片段翻译成 PHP 吗?我对这两种语言都不太熟悉 :(
matches = re.compile("\"cap\":\"(.*?)\"")
totalrewards = re.findall(matches, contents)
print totalrewards
这是上面代码的直接翻译,其中“contents”已填充以进行演示:
<?php
$contents = '"cap":"foo" "cap":"wahey"';
if (preg_match_all('/"cap":"(.*?)"/', $contents, $matches, PREG_SET_ORDER)) {
var_dump($matches);
}
输出:
array(2) {
[0]=>
array(2) {
[0]=>
string(11) ""cap":"foo""
[1]=>
string(3) "foo"
}
[1]=>
array(2) {
[0]=>
string(13) ""cap":"wahey""
[1]=>
string(5) "wahey"
}
}
<?php
$contents = '"cap":"foo" "cap":"wahey"';
if (preg_match_all('/"cap":"(.*?)"/', $contents, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
// array index 1 corresponds to the first set of brackets (.*?)
// we also add a newline to the end of the item we output as this
// happens automatically in PHP, but not in python.
echo $match[1] . "\n";
}
}
totalrewards
相当于$matches [0] [1]
。 - NullUserException