php isim ve e-posta adresi bölünmüş veya düzenli ifade kullanarak nasıl

3 Cevap php

Ben dize şu var:

"Test, User" < test@test.com >, "Another, Test" < another@test.com >, .........

Ben şu sonuç istiyorum:

array(
  array('name' => 'Test, User', 'email' => 'test@test.com'),
  array('name' => 'Another, Test', 'email' => 'another@test.com'),  
  ...........
)

3 Cevap

Ben oldukça tam ayrıştırıcı oluşturmak için başka bir ile burada bu cevabı kombine:

function parseEmailListToArray($list) {
    $t = str_getcsv($list);

    foreach($t as $k => $v) {
        if (strpos($v,',') !== false) {
            $t[$k] = '"'.str_replace(' <','" <',$v);
        }
    }

    foreach ($t as $addr) {
        if (strpos($addr, '<')) {
            preg_match('!(.*?)\s?<\s*(.*?)\s*>!', $addr, $matches);
            $emails[] = array(
                'email' => $matches[2],
                'name' => $matches[1]
            );
        } else {
            $emails[] = array(
                'email' => $addr,
                'name' => ''
            );
        }
    }

    return $emails;
}

Neden preg_split desen eşleştirme yok:

"Test, User" < test[at]test.com >,

Sonra preg_match isim ve e-posta bileşenleri bulmak için, daha sonra bir dizi koyun.