Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]preg_replace i polskie znaki
sirklima
post 12.04.2013, 13:41:26
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 19.01.2013

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


Witam,

Wiem ze temat juz byl poruszany kilkukrotnie, jednak mam pytanie zwiazane z preg_replace i z polskimi znakami.

Obecnie uzywam takiego rozwiazania:

  1.  
  2. $seoname = iconv('UTF-8', 'ASCII//TRANSLIT', $title_ouput);
  3. $seoname = preg_replace( "/[^a-z0-1-]{1}/i", '-', $seoname );
  4. $seoname = preg_replace('/\@/',' at ',$seoname);
  5. $seoname = preg_replace('/\&/',' and ',$seoname);
  6. $seoname = preg_replace('/\s[\s]+/','-',$seoname); // Strip off multiple spaces
  7. $seoname = preg_replace('/[\s\W]+/','-',$seoname); // Strip off spaces and non-alpha-numeric
  8. $seoname = preg_replace('/^[\-]+/','',$seoname); // Strip off the starting hyphens
  9. $seoname = preg_replace('/[\-]+$/','',$seoname); // // Strip off the ending hyphens


Polskie znaki sa ladnie zamieniane, jednak pomiedzy znakami laduje "-"
Czyli dla przykladu: Jak się zabezpieczyć przed powodzią - otrzymuje "jak-si-e-zabezpieczy-c-przed-powodzi-a" a chcialbym "uzyskac jak-sie-zabezpieczyc-przed-powodzia"


Prosze o pomoc, jak ktos ma jakies pomysly.

Dziekuje




Go to the top of the page
+Quote Post
matiit
post 12.04.2013, 13:43:08
Post #2





Grupa: Zarejestrowani
Postów: 365
Pomógł: 70
Dołączył: 5.04.2009

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


  1. function slugify($text)
  2. {
  3. // replace non letter or digits by -
  4. $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  5.  
  6. // trim
  7. $text = trim($text, '-');
  8.  
  9. // transliterate
  10. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  11.  
  12. // lowercase
  13. $text = strtolower($text);
  14.  
  15. // remove unwanted characters
  16. $text = preg_replace('~[^-\w]+~', '', $text);
  17.  
  18. if (empty($text))
  19. {
  20. return 'n-a';
  21. }
  22.  
  23. return $text;
  24. }


Źródło: http://stackoverflow.com/questions/2955251...slug-url-string
Go to the top of the page
+Quote Post
sirklima
post 12.04.2013, 14:58:28
Post #3





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 19.01.2013

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


Dziekuje bardzo, dziala.
Go to the top of the page
+Quote Post
matiit
post 12.04.2013, 15:07:16
Post #4





Grupa: Zarejestrowani
Postów: 365
Pomógł: 70
Dołączył: 5.04.2009

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


Jesli używasz tego jako slug, pamiętaj, aby w bazie mieć pole slug jako unique i dbać jakoś o tę unikalnośc. Np. jeśli taki slug już jest, to dodajesz do niego randomową liczbę, a najlepiej id posta na koniec.
Go to the top of the page
+Quote Post
sirklima
post 12.04.2013, 15:34:29
Post #5





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 19.01.2013

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


Mam to jako Unique, bo oczywiscie nie moze sie powtarzac. Dzieki.
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 Wersja Lo-Fi Aktualny czas: 5.07.2025 - 21:18