以下示例代码按预期工作(它到达if-block,不会进入else-block):
// 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
块。 任何想法可能出错?
答案 0 :(得分:0)
我需要使用自由间距标志:
'/name *?= *?"?(?P<filename>.*?)"? /ix'
(添加x
)