سؤال

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?

هل كانت مفيدة؟

المحلول

$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.
        )

)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top