次のサンプル コードは期待どおりに動作します (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
ブロックに到達していないはずです。何が間違っているのでしょうか?