PHP: imap_sort - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imap_status imap_setflag_full

Last updated: Fri, 26 Jan 2007

view this page in

# imap_sort

(PHP 3 >= 3.0.3, PHP 4, PHP 5)

imap_sort -- Gets and sort messages

## Описание

array imap_sort ( resource imap_stream, int criteria, int reverse [, int options [, string search_criteria [, string charset]]] )

Gets and sorts message numbers by the given parameters.

## Список параметров

`imap_stream`

  An IMAP stream returned by imap_open().

`criteria`

  Criteria can be one (and only one) of the following:

  - `SORTDATE` - message Date

  - `SORTARRIVAL` - arrival date

  - `SORTFROM` - mailbox in first From address

  - `SORTSUBJECT` - message subject

  - `SORTTO` - mailbox in first To address

  - `SORTCC` - mailbox in first cc address

  - `SORTSIZE` - size of message in octets

`reverse`

  Set this to 1 for reverse sorting

`options`

  The `options` are a bitmask of one or more of the following:

  - `SE_UID` - Return UIDs instead of sequence numbers

  - `SE_NOPREFETCH` - Don't prefetch searched messages

`search_criteria`

`charset`

## Возвращаемые значения

Returns an array of message numbers sorted by the given parameters.

## Список изменений

Версия | Описание
--- | ---
4.3.3 | The `charset` parameter was added

add a note User Contributed Notes
imap_sort
fili at fili dot nl
08-Feb-2007 01:54
`Just so you know, imap_sort apparently refuses to reverse SORTARRIVAL.
Scenario:
<?php
$result1 = imap_sort($mbox_id, SORTARRIVAL, false);
$result2 = imap_sort($mbox_id, SORTARRIVAL, true);
?>
Result:
$result1 and $result2 are identical
Solution:
$result = array_reverse($result);`
antoine dot spam-nono at maxg dot info
09-Feb-2006 07:42
`I worked a lot with IMAP functions since I wrote a complete webmail and I've got a little tip about the imap_sort function :
There is a big difference between :
<?php
imap_sort($imap, SORTDATE, 1);
// and
imap_sort($imap, SORTARRIVAL, 1);
?>
The first command will issue a
>> FETCH 1:last (UID ENVELOPE BODY.PEEK[HEADER.FIELDS (Newsgroups Content-MD5 Content-Disposition Content-Language Content-Location Followup-To References)] INTERNALDATE RFC822.SIZE FLAGS)
While the second resulted in
>> FETCH 1:last (UID INTERNALDATE RFC822.SIZE FLAGS)
As a result, using SORTDATE took 3 seconds longer to complete on a 800-emails mailbox, while the results are quite the same (except if you have to deal with forged dates or timezones, but the arrival order is far more logical)
My advice if you sort your emails by arrival is to actually use SORTARRIVAL, or better don't use imap_sort and go straight with message numbers (not UIDs). On large mailboxes, if you display messages per page, you will have significant performance increases (by avoiding 5 seconds of sorting).`
persian-horde at metanetworking dot com
07-May-2005 09:41
`Hello,
We used MIME and NLS libraries in our previous code(imap_locale_sort) that
are part of PEAR and Horde projects, and so our function
was so specific. Here is the general (pure php) code for this
function:
function imap_locale_sort($stream,$criteria,$reverse,$locale,$options)
{
if ($criteria!=SORTSUBJECT)
return (imap_sort($stream,$criteria,$reverse,$options));
$unsorted = array();
$sortresult = array();
$MC=imap_check($stream);
$MN=$MC->Nmsgs;
$overview = imap_fetch_overview($stream,"1:$MN",0);
$k=0;
while( list($key,$val) = each($overview))
{
$unsorted[$k]["uid"]=$val->uid;
$unsorted[$k]["subject"]=imap_utf8($val->subject);
$k++;
}
usort ($unsorted, create_function('$a,$b','setlocale(LC_ALL,$locale);return strcoll($a["subject"],$b["subject"]);'));
for ($m=0;$m<count($unsorted);$m++)
array_push($sortresult,$unsorted[$m]["uid"]);
if ($reverse)
$sortresult = array_reverse($sortresult);
return $sortresult;
}
Sample usage:
$mbox = imap_open("{localhost:143}INBOX.sent-mail","userid","password");
if ($mbox)
echo ("Connection Successful!");
$sorted = imap_locale_sort($mbox,SORTSUBJECT,0,'fa_IR',0);
print_r($sorted);
print ("\n\n");
$sorted = imap_sort($mbox,SORTSUBJECT,SE_UID);
print_r($sorted);
imap_close($mbox);`
persian-horde at metanetworking dot com
07-May-2005 03:17
`imap_sort uses the c-client library to sort messages. This library currently does not
support locale-based sort for foreign languages (Although it has charset option).
They are working on this(http://www.washington.edu/imap/IMAP-FAQs/#1.12),
but you can use the following function which uses strcoll() for locale-based sorting.
Pease note that this is required for SUBJECT field sorting, because most of
other fields are sorted correctly by imap_sort in any locale:
function imap_locale_sort($stream,$criteria,$reverse,$locale,$options)
{
if ($criteria!=SORTSUBJECT)
return (imap_sort($stream,$criteria,$reverse,$options));
$unsorted = array();
$sortresult = array();
$MC=imap_check($stream);
$MN=$MC->Nmsgs;
$overview = imap_fetch_overview($stream,"1:$MN",0);
$k=0;
while( list($key,$val) = each($overview))
{
$unsorted[$k]["uid"]=$val->uid;
$unsorted[$k]["subject"]=MIME::decode($val->subject, NLS::getCharset());
$k++;
}
usort ($unsorted, create_function('$a,$b','setlocale(LC_ALL,$locale);return strcoll($a["subject"],$b["subject"]);'));
for ($m=0;$m<count($unsorted);$m++)
array_push($sortresult,$unsorted[$m]["uid"]);
if ($reverse)
$sortresult = array_reverse($sortresult);
return $sortresult;
}
Usage example:
$sorted = imap_locale_sort($stream,SORTSUBJECT,0,'fa_IR',0);`
boscolau at mac dot com
22-Mar-2005 02:57
`in reply to graham_NOSPAM at rdb-concepts dot NOSPAM dot co dot uk here is a more elegant way to achieve sorting of search results:
function order_search($searchresults, $sortresults) {
return array_values(array_intersect($sortresults,$searchresults));
}
example of using function:
$sortresults = imap_sort($stream, $sort, $sortorder);
$searchresults = imap_search($stream, $query);
$sorted_search_results = order_search($searchresults, $sortresults);
sorted version of search results will then be held in the array of $sorted_search_results.`
tilmauder at yahoo dot com
27-Apr-2004 09:43
`in response to fprado's problem, the solution would be to use array_multisort() function on imap_fetchheader(). sdavey at datalink dot net dot au wrote an excellent example under array_multisort. I have used it to sort POP3 by message size. Unfortunately, my code is much too convoluted to post it here.`
fprado at pmovil dot com dot br
21-Nov-2002 01:23
`I use imap_sort() to go through POP3 accounts. However, sometimes, when the mailbox is too large, the response is incredibly slow.
I tried sniffing the network to discover the source of the problem and found that when imap_sort() is called for a POP3 server, it downloads the full body & attachs of ALL the messages in the mailbox. Since my POP server and my Web server are on differente machines, this caused the considerably slow response.
I've been trying a work-around this issue and if I manage to do this I'll post it here.`
graham_NOSPAM at rdb-concepts dot NOSPAM dot co dot uk
18-Feb-2002 01:34
`for those of you with an old version of PHP that doesn't support the extra search paramater you may want to use this function:
function order_search($searchresults, $sortresults) {
$searchhash = array();
$returnresults = array();
$count = 0;
for ($i = 0; $i < sizeof($searchresults); $i++) {
$searchhash[$searchresults[$i]] = true;
}
for ($i = 0; $i < sizeof($sortresults); $i++) {
if (@$searchhash[$sortresults[$i]]) {
$returnresults[$count] = $sortresults[$i];
$count++;
}
}
return $returnresults;
}
example of using function:
$sortresults = imap_sort($stream, $sort, $sortdir, SE_UID);
$searchresults = imap_search($stream, $search, SE_UID);
$results = order_search($searchresults, $sortresults);
$results will then hold a sorted array of search results.`
naryaATtolkiendilDOTcom
09-Jan-2002 08:52
`I've discovered while hacking IMP that imap_sort() allow you to define search options :
imap_sort ($stream, $criteria, $reverse, SE_UID, $search);
where $search is an imap_search() query.`
ks@edv
06-Oct-2000 03:18
`just finished:
include('imappop3.php');
$mbox=imap_open($mailbox,$username,$password) or die('keine Verbindung!');
//sortiert reverse nach Gr��e und liefert UIDs
$sorted_array=imap_sort($mbox,SORTSIZE,1,SE_UID);
$mail=imap_fetchstructure($mbox,next($sorted_array),FT_UID);
echo $mail->bytes.'<br>';
$mail=imap_fetchstructure($mbox,end($sorted_array),FT_UID);
echo $mail->bytes.'<br>';
if ($err=imap_errors()) print_r($err);
imap_close ($mbox);
outputs the size of the biggest and smallest EMail:
36476
526`
add a note
imap_status imap_setflag_full

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST