Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] strtolower; ucfirst likwidują polskie znaki ;/, jak temu zaradzić?
DeBest
post
Post #1





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 20.10.2006

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


Witam,

zrobiłem ostatnio funkcję w php która poprawia wpisane dane tak aby zamiast wpisanych np. "DaWId" było "Dawid".

I wszystko ok ale...
Jeśli ktoś ma polskie znaki diakrytyczne w imieniu to zamiast nich są kwadraty ;/

oto kod tej funkcji:
  1. <?php
  2. function convert($name) {
  3.  
  4. $string = split(" ", $name);
  5. $string_lower = strtolower($string[0]);
  6. $string_ucfirst = ucfirst($string_lower);
  7.  
  8. return $string_ucfirst;
  9. ?>


Wie ktoś jak temu zaradzić?
Go to the top of the page
+Quote Post
mike
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


A tak Cie naprowadzę:
  1. <?php
  2.  
  3. function ucfirst_utf8($str) {
  4.    if (mb_check_encoding($str,'UTF-8')) {
  5.        $first = mb_substr(mb_strtoupper($str, "utf-8"),0,1,'utf-8');
  6.        return $first.mb_substr(mb_strtolower($str,"utf-8"),1,mb_strlen($str),'utf-8');
  7.    } else {
  8.        return $str;
  9.    }
  10. }
  11.  
  12. ?>
Go to the top of the page
+Quote Post
.radex
post
Post #3





Grupa: Zarejestrowani
Postów: 1 657
Pomógł: 125
Dołączył: 29.04.2006

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


po pierwsze - jakie kodowanie? jeśli utf-8, to będziesz musiał użyć funkcji mb_*

po drugie - php AFAIK nie obsługuje przy strtolower itp. polskich znaków, więc będziesz musiał sobie sam zamienić poprzez str_replace('Ę','ę',$string); itd.

EDIT: Ok, mike zrobił już to za mne tongue.gif

Ten post edytował .radex 18.02.2009, 11:02:03


--------------------
blog | Tadam — minutnik do Pomodoro na Maka :)
Go to the top of the page
+Quote Post
mike
post
Post #4





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


Cytat(.radex @ 18.02.2009, 11:01:36 ) *
EDIT: Ok, mike zrobił już to za mne tongue.gif
Aaa tam zaraz ~mike. Wystarczy manuala dobrze czytać. Oo, kolejny przykład: link.
Go to the top of the page
+Quote Post
DeBest
post
Post #5





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 20.10.2006

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


używam UTF-8

niestety wyskakuje mi komunikat:

Cytat
Fatal error: Call to undefined function mb_check_encoding() in c:\usr\krasnal\www\pkzneta\pk\functions.php on line 82


testowałem to jak na razie na Krasnalu na PHP 4 i PHP5 i ciągle to samo
Go to the top of the page
+Quote Post
mike
post
Post #6





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


No pomyśl trochę. Co może oznaczać że nie masz takiej funkcji jak mb_check_encoding()?
Pewnie to, że nie masz żadnej funkcji z biblioteki Multibyte String tongue.gif
Ojej, a to oznacza, że nie masz w ogóle całej biblioteki. Jaki wniosek? Nalezy sobie ją zainstalować: Multibyte String > Instalacja
Go to the top of the page
+Quote Post
DeBest
post
Post #7





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 20.10.2006

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


do tego że tej biblioteki to doszedłem ;P

a np gdyby na moim hostingu nie było tej biblioteki to czy istnieje inna metoda na zrobienie tego skryptu?
Go to the top of the page
+Quote Post
mike
post
Post #8





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


Cytat(DeBest @ 18.02.2009, 12:56:20 ) *
a np gdyby na moim hostingu nie było tej biblioteki to czy istnieje inna metoda na zrobienie tego skryptu?
Nie. Bo PHP jest zbyt ułomne na UTF-8 domyślnie.
Go to the top of the page
+Quote Post
8rol
post
Post #9





Grupa: Zarejestrowani
Postów: 66
Pomógł: 0
Dołączył: 10.10.2009

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


Cytat(mike @ 18.02.2009, 10:59:12 ) *
A tak Cie naprowadzę:
  1. <?php
  2.  
  3. function ucfirst_utf8($str) {
  4.    if (mb_check_encoding($str,'UTF-8')) {
  5.        $first = mb_substr(mb_strtoupper($str, "utf-8"),0,1,'utf-8');
  6.        return $first.mb_substr(mb_strtolower($str,"utf-8"),1,mb_strlen($str),'utf-8');
  7.    } else {
  8.        return $str;
  9.    }
  10. }
  11.  
  12. ?>



Sorry, że trochę odkopuję temat, ale rozglądam się za czymś podobnym i czy czasem prawidłowo nie powinno być tak:

  1. <?php
  2.  
  3. function ucfirst_utf8($str) {
  4. if (mb_check_encoding($str,'UTF-8')) {
  5. $first = mb_substr(mb_strtoupper($str, "utf-8"),0,1,'utf-8');
  6. return $first.mb_substr(mb_strtolower($str,"utf-8"),1,(mb_strlen($str)-1),'utf-8');
  7. } else {
  8. return $str;
  9. }
  10. }
  11.  
  12. ?>


Zamiast mb_strlen($str) to (mb_strlen($str)-1).
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: 20.08.2025 - 06:05