0

I need to test an object property for equivalence to a string.

I'm using the php imap functions to cycle through the 10 most recent emails in a gmail account, I want to filter them so I'm only dealing with email from twitter. This for loop is the relevant extract of code where things are going wrong.

for ($currentMsgno; $currentMsgno > (msgnoTotal-10); $currentMsgno--){
  echo "MESSAGE NO IS : " . $currentMsgno . "<br/>";

  $headerObj = imap_headerinfo($mbox,$currentMsgno,0,0);

  if ($headerObj->fromaddress=='Twitter'){
      echo "MESSAGE FROMADDRESS IS: " . $headerObj->fromaddress . "<br/>"
  }
  imap_setflag_full($mbox, "$currentMsgno", "\\Seen");
}

All of the code works okay apart from trying to test ($headerObj->fromaddress=='Twitter'), this simply doesn't work. I'm not sure if there is something very basic that i'm not doing when dealing with the objects property... is there a funciton I should be using?

Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29
schnimmy
  • 43
  • 3
  • 9
  • 1
    Are you sure the value is 'Twitter'? Would it not be something more like 'Twitter '? – Dale Jun 09 '13 at 22:23
  • 1
    `var_dump($headerObj->fromaddress)` to check what's really here. Perhaps you need to test (with `preg_match` or `strpos`, depending on conditions) for some specific part(s) of address instead. – raina77ow Jun 09 '13 at 22:25
  • @Dale , I'm quite sure because if I take away the if test and just echo $headerObj->fromaddress it comes up as Twitter unless another tweeter has contacted me in which case it comes up as Tweeter Name(Twitter). – schnimmy Jun 10 '13 at 13:38
  • @raina77ow the var_dump returns string(67) "Twitter " – schnimmy Jun 10 '13 at 14:24
  • also i've assigned the property to a variable like so "$objProperty = $headerObj->fromaddress", and the if test now looks like ($objProperty == "Twitter ") to reflect the trailing space that shows up in the var_dump. and it still doesnt work... – schnimmy Jun 10 '13 at 14:27
  • The string you compare to is _67_ characters long, that's what `var_dump` result means. Get rid of whitespace completely, by something like `$objProperty = trim($headerObj->fromaddress)`, only then compare it. – raina77ow Jun 10 '13 at 15:02
  • $objProperty = trim($headerObj->fromaddress) didn't work for some reason, because i did a var_dump on $objProperty afterwards and it still said string(67). However i changed the if test to (preg_match(/twitter/i, $objProperty)) and it works now, so thanks @raina77ow for suggesting that. – schnimmy Jun 10 '13 at 16:28

0 Answers0