Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] switch zawiera, a nie równa się., odpowiednik preg_match
tenloginjestzaje...
post
Post #1





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

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


Witam
Muszę zrobić różne anchory stosownie do słów w zmiennej $url. Jak to zrobić używając preg_match lub switch
  1. $default = 'wspolny anchor';
  2. if(preg_match('/jeden/', $url)) {$anchor = 'jeden';} else {$anchor = $default;}
  3. if(preg_match('/dwa/', $url)) {$anchor = 'dwa';} else {$anchor = $default;}
  4. ...
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Słabo doprecyzowałeś ale masz. dostaniesz 1-szy znaleziony anchor z listy.

  1. <?php
  2.  
  3. $anchor = 'Default text';
  4. $anchorList = ['jeden', 'dwa'];
  5. foreach ($anchorList as $item) {
  6. if(mb_stripos($url, $item) !== false) {
  7. $anchor = $item;
  8. break;
  9. }
  10. }
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 2.10.2025 - 17:21