1

很抱歉问了一个这么简单的问题,但这是我的问题。我实际上正在修改 PlancakeEmailParser 以满足我的需要。

我做了一个简单的正则表达式:

/([^:]+) (<+.*>)$/

应该匹配:

John DOE <johndoe@fakemail.com>

我在http://www.gethifi.com/tools/regex中对其进行了测试,它运行良好

但在我的代码中它返回 false !

这是有问题的代码:

preg_match('/([^:]+): ?(.*)$/', $line, $matches);
$newHeader = strtolower($matches[1]);

$string = $matches[2];
$reg = "/([^:]+) (<+.*>)$/";

echo 'VALUE: '.$string."\n";
echo 'PREG_MATCH: '.preg_match($reg, $string)."\n\n";

if (preg_match('/([^:]+) (<+.*>)$/', $matches[2])) {
    echo('match'."\n\n\n\n");
} else {
    $value = $matches[2];
}

这是回声反馈:

VALUE: John DOE <johndoe@fakemail.com>
PREG_MATCH: 0

你们中有人知道问题出在哪里吗?

非常感谢 !

编辑

问题是我在所有内容上创建了一个 htmlentities,所以 <> 在哪里转换。

解决方案是从此重写正则表达式:

/([^:]+) (<+.*>)$/

对此

/([^:]+) (&lt;+.*&gt;)$/

非常感谢 Peter Alfvin 提供的帮助 :)

也许有一天它可以帮助某人

4

1 回答 1

0

已解决问题是我在所有内容上创建了一个htmlentities,所以 <> 转换了。解决方案是从此重写正则表达式:

/([^:]+) (<+.*>)$/ to this /([^:]+) (&lt;+.*&gt;)$/

或从所有内容中删除 htmlentities 并仅在需要时使用它,我终于做到了!

也许有一天它可以帮助某人......

于 2013-08-13T13:19:56.313 回答