preg_match with Zend_Mail_Part->getContent()

184 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

I needed to use the free spacing flag:

'/name *?= *?"?(?P<filename>.*?)"? /ix'

(Add the x)