0

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.

DJB
  • 257
  • 2
  • 11
  • As a temp solution I'm using: if (strpos($line,$folder) !== false && preg_match('/\(([A-Za-z0-9 ]+?)\)/', $line, $match)) { But it's not perfect! – DJB Feb 11 '14 at 22:20

1 Answers1

0

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)) {}
B. Martin
  • 1,047
  • 12
  • 18
  • works perfectly! Only problem I have now is I've realised I can't find the uid of sent items from fMailbox... but it was helpful anyway! thank you – DJB Feb 11 '14 at 22:46
  • What text do you have? Which value do you need to get? – B. Martin Feb 11 '14 at 22:47
  • fMailbox doesn't use php's imap class, and as such doesn't seem to obtain the server designated message uid, but simply guesses what the message id should be by its order in the mailbox, which makes sense when looking at an inbox on the whole - but if limiting to a single folder, you end up with entirely wrong numbers. Your preg_match worked brilliantly though! got the right data – DJB Feb 12 '14 at 00:28