Question

I have only just come across preg_match (that's a lie, I've been avoiding it like the plague).. and now I have to fix an expression in a library I have been using.

The library in question is flourish, and I have checked and the license is MIT so no worries about editing the source. I am currently using only the fMailbox class, and editing the class to allow use of getting emails from different folders. The original was not designed for this, and has syntax:

$messages = $mailbox->listMessages($limit,$page);
$messages = $mailbox->listMessages(5,0); // for example - the first 5 emails in the inbox

Now I am editing so that I can get:

$messages = $mailbox->listMessages($limit,$page = NULL, $sortorder, $folder);

It appears someone else has done the same/similar and I have got the $sortorder working but when changing folder (to, for example: '[Google Mail]/Sent Items') the following preg_match does not get any matches and I get the wrong information (it works fine if folder = 'INBOX');

if (preg_match('#^\s*\*\s+STATUS\s+"?'.$folder.'"?\s+\((.*)\)$#', $line, $match)) {}
// where $folder = '[Google Mail]/Sent Mail'

The string it should be looking at ($line) is: '* STATUS "[Google Mail]/Sent Mail" (MESSAGES 528)'

If I had to guess it is the spaces in the folder name that causes it to fail, but that really is a guess.

Could someone kindly explain how to match the 'MESSAGES 528' part of the string by preg_match? As I think this is the only problem I have to getting the class to work successfully.

Any help is much appreciated.

Was it helpful?

Solution

Regular expressions are my friends ;-) You have to use preg_quote if you want to integrate a string with special chars into your regex pattern. Try this this:

if (preg_match('#^\s*\*\s+STATUS\s+"?'.preg_quote($folder).'"?\s+\((.*)\)$#', $line, $match)) {}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top