2

I have this MX output in $ip:

10 ASPMX2.GOOGLEMAIL.COM. 10 ASPMX3.GOOGLEMAIL.COM. 1 ASPMX.L.GOOGLE.COM. 5 ALT1.ASPMX.L.GOOGLE.COM. 5 ALT2.ASPMX.L.GOOGLE.COM.

The number is the priority and the subdomain is the mail server. How could I stored them in array like this:

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => ASPMX2.GOOGLEMAIL.COM.
        )
    [1] => Array
        (
            [0] => 10
            [1] => ASPMX3.GOOGLEMAIL.COM.
        )
...
)

The hard part is the whole output could be anything. I mean the mail server subdomain name and the number of server could be random. In the above is 5 mail server but it could be 3 or just 1 server (not to be confuse with mail server priority number).

I'm thinking about preg_match, but the random subdomain name just leaves me clueless. Any idea?

sg552
  • 1,521
  • 6
  • 32
  • 59

1 Answers1

2
$arr = array();

preg_match_all('/(\d+) ([\w.\-]+)/', $ip, $matches);
for($i = 0; $i < count($matches[1]); $i++)
{
    $arr[] = array($matches[1][$i], $matches[2][$i]);
}
Array
(
    [0] => Array
        (
            [0] => 10
            [1] => ASPMX2.GOOGLEMAIL.COM.
        )

    [1] => Array
        (
            [0] => 10
            [1] => ASPMX3.GOOGLEMAIL.COM.
        )

    [2] => Array
        (
            [0] => 1
            [1] => ASPMX.L.GOOGLE.COM.
        )

    [3] => Array
        (
            [0] => 5
            [1] => ALT1.ASPMX.L.GOOGLE.COM.
        )

    [4] => Array
        (
            [0] => 5
            [1] => ALT2.ASPMX.L.GOOGLE.COM.
        )

)
  • I add `0 see-why.net.` in `$ip` and the array output is: `[1] => see` As you can see the `-why.net.` is missing. Help me with the `-` problem please? – sg552 Feb 05 '12 at 20:40
  • @sg552: I've updated my answer. You just needed to add `-` to the character class. –  Feb 05 '12 at 20:55