2016-03-21 25 views
0

Bir dizinin belirli bir dizeler dizisi içerip içermediğini görmeye çalışıyorum. Benim özel durumumda, müşteri adreslerini içeren bir dizim var. Her adresin bir PO Box olup olmadığını görmeye çalışıyorum. Tüm adresleri PO Box ise bir hata mesajı yazdırmak istiyorum.PHP Bkz. Dizideki Tüm Değerler Dizin İçeriyor Var

Şu anda sahip olduğum şey budur. İşte

public function checkPhysicalAddressOnFile(){ 
    $customer = Mage::getSingleton('customer/session')->getCustomer(); 
    foreach ($customer->getAddress() as $address) { 
     if stripos($address, '[p.o. box|p.o box|po box|po. box|pobox|post office box]') == false { 
      return false 
+0

Ben en iyi yanıt olarak işaretlenen


Burada daha kısa ve öz seçeneğimiz! Teşekkürler! – djames

cevap

0

ben yaklaşım şekli şöyledir:

public function checkPhysicalAddressOnFile(){ 
    $addresses = Mage::getSingleton('customer/session')->getCustomer()->getAddresses(); 

    foreach($addresses AS $address) { 
     if(!preg_match("/p\.o\. box|p\.o box|po box|po\. box|pobox|post office box/i", $address)) { 
      // We found an address that is NOT a PO Box! 
      return true; 
     } 
    } 

    // Apparently all addresses were PO Box addresses, or else we wouldn't be here. 
    return false; 
} 

Kodunuz zaten çalışma oldukça yakındı, eğer çok sayıda sadece regex deseni karşı kontrol etmek preg_match fonksiyonunu gerekli.

public function checkPhysicalAddressOnFile() { 
    return (bool) count(array_filter(Mage::getSingleton('customer/session')->getCustomer()->getAddresses(), function($address) { 
     return !preg_match("/p\.o\. box|p\.o box|po box|po\. box|pobox|post office box/i", $address); 
    })); 
} 

bir örnek için buraya bakın: https://3v4l.org/JQQpA

İlgili konular