解析PHP IMAP扩展等主机信息

I have been trying to get the host information (hostname, port, prefix...) like PHP IMAP extension does, but I can't figure out. example string:

$host = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX'

The closer I came up was PHPMailer does (code taken from source code):

$hostinfo = array();
if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) {
    die(' Not a valid host entry');
}
// $hostinfo[2]: optional ssl or tls prefix
// $hostinfo[3]: the hostname
// $hostinfo[4]: optional port number
// The host string prefix can temporarily override the current setting for SMTPSecure
// If it's not specified, the default value is used

But I can't get there.

Any help will be much appreciated.

Try this one:

<?php
  $sourcestring='{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
  preg_match_all('/{((?<proto>[^:]+?):\/\/)?(?<host>[^:\/]+?)(:(?<port>\d+?))?\/(?<path>[^}]+?)?}(?<folder>.+)/m',$sourcestring,$matches);
  echo "<pre>".print_r($matches,true);
?>

It captures the parts as named groups. You can get the parts by their names ('proto', 'host', 'port', 'path' and 'folder').

(It's just a quickshot. So let me know, if something still doesn't fit.)