以下示例代码按预期工作(它到达 if 块,不进入 else 块):
// In the real code, $content is a result of Zend_Mail_Part->getContent() (this method returns a string).
$content = ' ----boundary_2_1dca5b3b-499e-4109-b074-d8b5f914404a Content-Type: application/octet-stream; name=abc.pdf Content-Transfer-Encoding: base64 JVBERi0xLjINJeLjz9MNCjIgMCBvYmo8PD4+DWVuZG9iag0zIDAgb2JqPDwvUG';
// NOTE: the regex has a trailing space
// name *?= *?"?(?<filename>.*?)"?
if (preg_match('/name *?= *?"?(?P<filename>.*?)"? /i', $content, $matches))
{
echo ('file name: ' . $matches['filename']);
}
else
{
echo('<br>$content:<br/>');
var_dump($content);
echo('<br>preg_match result:<br/>');
var_dump(preg_match('/name *?= *?"?(?P<filename>.*?)"? /i', $content, $matches));
echo('<br>$matches:<br/>');
var_dump($matches);
}
然而,当$content
被赋值为 的结果值时Zend_Mail_Part->getContent()
,代码的执行会到达该else
块。它不应该到达else
街区。有什么想法可能是错的吗?