Preg_match与PHP中的引用变量匹配


Preg_match with referenced variable in PHP

下面的代码没有给我预期的结果。

if (isset($newpost['message']))
{
    $matches = array();
    preg_match_all("~'[QUOTE'=.*;('d+)']~isU", $newpost['message'], $matches);
    var_dump($matches);
    die();
}

$matches应该包含匹配的结果。但是,$matches总是等于整个$newpost数组(而不仅仅是消息部分)。上面的结果可能像这样:

array(22) { ["message"]=> string(17) "testing123..." ["title"]=> &string(0) "" ["iconid"]=> &int(0) ["parseurl"]=> bool(true) ["signature"]=> &int(1) ["preview"]=> &string(0) "" ["disablesmilies"]=> &int(0) ["rating"]=> &int(0) ["username"]=> &string(0) "" ["folderid"]=> &int(0) ["quickreply"]=> &int(0) ["poststarttime"]=> &int(1368357609) ["posthash"]=> &string(32) "4d513f4123f780c6b10739e3a5dd0fb6" ["humanverify"]=> &array(0) { } ["stickunstick"]=> &int(0) ["openclose"]=> &int(0) ["ajaxqrfailed"]=> int(0) ["emailupdate"]=> &int(9999) ["enablesmilies"]=> int(1) ["podcastsize"]=> int(0) ["visible"]=> int(1) ["postid"]=> int(1771567) }

我认为这是因为$newpost可能被当作引用来处理。

我在Ideone上测试了这段代码,它运行正常。

代码:

<?php
$text = <<<EOD
abc [QUOTE=ABC;123]
def [Quote=DEF;456]
ghi
EOD;
$newpost = array('message' => $text);
if (isset($newpost['message'])) {
    preg_match_all('/''[QUOTE=[^;]++;(''d++)'']/i', $newpost['message'], $matches);
    var_dump($matches);
}
结果:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(15) "[QUOTE=ABC;123]"
    [1]=>
    string(15) "[Quote=DEF;456]"
  }
  [1]=>
  array(2) {
    [0]=>
    string(3) "123"
    [1]=>
    string(3) "456"
  }
}

输出来自脚本的其他地方…