PHP - imap_rfc822_parse_adrlist() 函式



PHP 的 IMAP 函式可以幫助您訪問電子郵件帳戶,IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol,使用這些函式您還可以處理 NNTP、POP3 協議和本地郵箱訪問方法。

imap_rfc822_parse_adrlist() 函式接受兩個字串值(地址和預設主機名)作為引數,並解析給定的地址字串。

語法

imap_rfc822_parse_adrlist($address, $default_host);

引數

序號 引數及描述
1

address (必填)

這是一個表示地址的字串值。

2

default_host (必填)

這是一個表示預設主機名的字串值。

返回值

此函式返回一個包含已解析值的陣列物件。

PHP 版本

此函式首次引入於 PHP 4 版本,並在所有後續版本中均有效。

示例

以下示例演示了 imap_rfc822_parse_adrlist() 函式的用法:

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
		 
         //Parsing a string address	 
         print("Parsing string address: "."<br>");
         $addr_str = "Sender <sender@test.com>, CC@test.com, root";
         $res = imap_rfc822_parse_adrlist($addr_str, "default_host");
         print_r($res);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Parsing string address:
Array ( 
   [0] => stdClass Object ( 
      [mailbox] => sender [host] => test.com [personal] => Sender 
   ) 
   [1] => stdClass Object ( 
      [mailbox] => CC [host] => test.com 
   ) 
   [2] => stdClass Object ( 
      [mailbox] => root [host] => default_host 
   ) 
)

示例

以下是該函式的另一個示例:

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
         
         //Parsing a string address	 
         print("Parsing string address: "."<br>");
         $addr_str = "Sender <sender@test.com>, CC@test.com, root";
         $res = imap_rfc822_parse_adrlist($addr_str, "default_host");
         foreach ($res as  $val) {
         print($val -> mailbox);
            print("<br>");
            print($val -> host);
            print("<br>");
         }
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Parsing string address:
sender
test.com
CC
test.com
root
default_host
php_function_reference.htm
廣告