The following sample code works as expected (it reaches the if-block, does not go in the 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);
}
However, when $content
is assigned the resulting value of Zend_Mail_Part->getContent()
, the execution of the code reaches the else
block. It shouldn't be reaching the else
block. Any ideas what might be wrong?
I needed to use the free spacing flag:
(Add the
x
)