Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]preg_match tablica
inomi13
post
Post #1





Grupa: Zarejestrowani
Postów: 95
Pomógł: 0
Dołączył: 16.08.2017

Ostrzeżenie: (0%)
-----


Mam poniższy kod, który sprawdza czy adres e-mail pasuje do danej domeny. Jednak mam problem z przerobieniem kodu tak aby sprawdzał czy tablica adresów e-mail występuje w tablicy domen. Proszę o sugestie jak się do tego zabrać bo wiem że preg_match sprawdza tylko dane ciągi a nie tablice.

  1. $example = array('sklep@o.pl','sklep@i.pl','sklep@w.pl');
  2. $searchword = ('o.pl');
  3. foreach($example as $index=>$value)
  4. {
  5. if(preg_match("/$searchword/i", $example[$index]))
  6. {
  7. echo $example[$index];
  8. }
  9. }


Ten post edytował inomi13 9.02.2021, 10:21:11
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
trueblue
post
Post #2





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Musisz po prostu dodać drugi for.

P.S. Sugeruję Ci dodanie do wyrażenia regularnego znaku @.


--------------------
Go to the top of the page
+Quote Post
inomi13
post
Post #3





Grupa: Zarejestrowani
Postów: 95
Pomógł: 0
Dołączył: 16.08.2017

Ostrzeżenie: (0%)
-----


Coś w tym rodzaju?

  1. $example = array('sklep@o.pl','sklep@i.pl','sklep@w.pl');
  2. $searchword = count(array('w.pl','o.pl'));
  3. foreach($example as $index=>$value)
  4. {
  5. for($i=1; $i<=$searchword; $i++)
  6. {
  7. if(preg_match("/$searchword[$i]/i", $example[$index]))
  8. {
  9. echo $example[$index];
  10. }
  11. }
  12. }
  13. }
Go to the top of the page
+Quote Post
trueblue
post
Post #4





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Może tak, prościej:

  1. $searchwords = array('w.pl','o.pl');
  2.  
  3. foreach($example as $value)
  4. {
  5. for($searchwords as $searchword)
  6. {
  7. if(preg_match("/@$searchword/i", $value))
  8. {
  9. echo $value;
  10. }
  11. }
  12. }



--------------------
Go to the top of the page
+Quote Post
YourFrog
post
Post #5





Grupa: Zarejestrowani
Postów: 124
Pomógł: 22
Dołączył: 10.01.2014

Ostrzeżenie: (0%)
-----


Jak już masz regexp pfu pfu (zobacz sobie w RFC jak wygląda regexp na prawidłowy adres e-mail). To skorzystaj z niego

  1. <?php
  2.  
  3. $domains = ['w.pl','o.pl'];
  4. $emails = ['sklep@o.pl','sklep@i.pl','sklep@w.pl'];
  5. $regexp = '/(' . implode('|', $domains) . ')/i';
  6.  
  7. $result = [];
  8. foreach($emails as $key => $email) {
  9. $result[$email] = (bool) preg_match($regexp, $email);
  10. }
  11.  
  12. var_dump($result);


Ten post edytował YourFrog 9.02.2021, 11:30:27
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 07:56