[英]Convert a Python snippet to PHP?
Can anyone translate my small Python snippet to PHP? 任何人都可以将我的小Python片段翻译成PHP吗? I'm not a familiar with both languages.
我不熟悉这两种语言。 :(
:(
matches = re.compile("\"cap\":\"(.*?)\"")
totalrewards = re.findall(matches, contents)
print totalrewards
Thank you for those who'd help! 感谢那些帮助的人! :(
:(
This is a direct translation of the code above, with "contents" populated for demonstration purposes: 这是上面代码的直接翻译,其中填充了“内容”以用于演示目的:
<?php
$contents = '"cap":"foo" "cap":"wahey"';
if (preg_match_all('/"cap":"(.*?)"/', $contents, $matches, PREG_SET_ORDER)) {
var_dump($matches);
}
The output: 输出:
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"
}
}
If you want to actually do something with the result, for eg list it, try: 如果你想真正对结果做些什么,例如列出它,试试:
<?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";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.