PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Beginner problem.
I have a little piece of code that will search a mailbox for the string "hello" within the subject of unread messages. It will display which message number the string lies in, if any. My problem lies in the "number of messages found" and the "match found in."
If 14 messages are found with the string it will display each message # that it is found in as well as a total of 14.
If 0 messages are found with the string it will display "0 Match found in number:" This is fine because I will not need this line, it's just for debugging purposes.
The problem is that the "Number of Messages Found:" will display 1, which would be incorrect.
Any suggestions? I've looked around quite a bit and can't find the answer. Everything I seem to do that corrects the incorrect count of 1 will subtract 1 from any answer that is correct. Thanks for your time.
[syntax="php"]
//open imap stream
$mail = imap_open("{pop.server.com}INBOX","user","password");
// search only in the subject of unread messages for the string 'hello'
$search_mail = imap_search($mail, "UNSEEN SUBJECT hello");
for ($i=0; $i<count($search_mail); $i++) {
echo "$i Match found in number: $search_mail[$i]<BR>\n";
}
echo "<br /><b>Number of Messages Found: $i</b><br /><br />";
imap_close($mail);
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hey, thanks for the reply. I tried that and it still wouldn't return what I needed it to. I managed to figure out a way around it. I added an if statement belolw which would set the variable $max to zero if the search returned an empty array.
//open imap stream
$mail = imap_open("{pop.server.com}INBOX","user","password");
// search only in the subject of unread messages for the string 'hello'
$search_mail = imap_search($mail, "UNSEEN SUBJECT hello");
for ($i=0; $i<count($search_mail); $i++) {
echo "$i Match found in number: $search_mail[$i]<BR>\n";
}
if ( $search_mail == "" ) {
$i = 0;
}
echo "<br /><b>Number of Messages Found: $i</b><br /><br />";
imap_close($mail);
When checking the mail, it seemed to work perfect. I'll try your suggestion and if it works, I 'll use it. I'd rather use the right way that works as opposed to the wrong way that works.
I have another question regarding IMAP connections that I can't seem to find anywhere. Is there a way to keep the connection alive and switch users on the server? It seems really slow loggin in as 7 different accounts on by one. If I don't use imap_close($mailserver); after checking each account would it be faster switching accounts?
Also, how would you search a string in the subject of a message? Say I have an e-mail with the subject "Hello World".
// search for replies
$search_replies = imap_search($mbox, "UNSEEN SUBJECT hello world");
if ( false!==$search_replies ) {
for ($replies=0; $replies<count($search_replies); $replies++) {
}
}
else { // return zero if search is false
$replies = 0;
}
I use the above code to search for e-mails with "hello world" in them. This code, however, will make $replies = 0. What am I missing to allow it to search strings of text?