Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] str_replace() nie działa w $_POST, przy zwykłych zmiennych śmiga normalnie
Manfred
post 1.04.2010, 12:12:01
Post #1





Grupa: Zarejestrowani
Postów: 45
Pomógł: 0
Dołączył: 21.03.2005
Skąd: Olsztyn

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


Witam.

Mam problem z funkcją str_replace(). Normalne zmienne $string działają (zamiana ó na o, spacje na "-" itp.), ale gdy chce tę funkcję wykorzystać do $_POST['string'] to w ogóle nie zamienia tych znaków, zostawia tak jak jest.

p1.php
  1. <form action="poligon.php" method="post">
  2. <p><label>Tytuł ogłoszenia:</label><input type="text" class="text-long" name="post" /></p>
  3. </form>


poligon.php
  1. <?php
  2.  
  3. function ogonki_Zamiana($text){
  4. $text=str_replace('ę', 'e', $text );
  5. $text=str_replace('ó', 'o', $text );
  6. $text=str_replace('ą', 'a', $text );
  7. $text=str_replace('ś', 's', $text );
  8. $text=str_replace('ł', 'l', $text );
  9. $text=str_replace('ż', 'z', $text );
  10. $text=str_replace('ź', 'z', $text );
  11. $text=str_replace('ń', 'n', $text );
  12. $text=str_replace('ć', 'c', $text );
  13. $text=str_replace('!', '', $text );
  14. $text=str_replace('?', '', $text );
  15. $text=str_replace('*', '', $text );
  16. $text=str_replace('"', '', $text );
  17. $text=str_replace('<', '', $text );
  18. $text=str_replace('>', '', $text );
  19. $text=str_replace(')', '', $text );
  20. $text=str_replace('(', '', $text );
  21. $text=str_replace('!', '', $text );
  22. $text=str_replace('=', '', $text );
  23. $text=str_replace('+', '', $text );
  24. $text=str_replace('-', ' ', $text );
  25. $text=str_replace(';', '', $text );
  26. $text=str_replace(':', '', $text );
  27. $text=str_replace(',', '', $text );
  28. $text=str_replace('.', '', $text );
  29. $text=str_replace('_', '-', $text );
  30. $text=str_replace('{', '', $text );
  31. $text=str_replace('}', '', $text );
  32. $text=str_replace(']', '', $text );
  33. $text=str_replace('|', '', $text );
  34. return $text;}
  35.  
  36. $zmienna = "łść";
  37. $zmienna = ogonki_Zamiana($zmienna);
  38.  
  39. echo "Zwykla zmienna: " . $zmienna;
  40. echo "<br />";
  41. echo "<br />";
  42.  
  43. $post = $_POST['post'];
  44. $post = ogonki_Zamiana($post);
  45. echo "Post: " . $_POST['post']; // tu nic się nie zmienia, a teoretycznie powinno
  46. echo "<br />";
  47.  
  48. ?>
Go to the top of the page
+Quote Post
nospor
post 1.04.2010, 12:14:11
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




nie: echo "Post: " . $_POST['post'];
a: echo "Post: " . $post;

mysl,mysl,mysl.... winksmiley.jpg


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Pawel_W
post 1.04.2010, 12:15:17
Post #3





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


Cytat(Manfred @ 1.04.2010, 13:12:01 ) *
  1. $post = $_POST['post'];
  2. $post = ogonki_Zamiana($post);
  3. echo "Post: " . $_POST['post']; // tu nic się nie zmienia, a teoretycznie powinno

chyba chodziło ci o
  1. echo "Post: " . $post; // tu nic się nie zmienia, a teoretycznie powinno


EDIT:
i tak w ogóle to do zamiany ogonków wystarczy
  1. $string = 'ąćęłńóśżź';
  2. echo $string.'<br />';
  3. $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
  4. echo $string;


Ten post edytował Pawel_W 1.04.2010, 12:18:06
Go to the top of the page
+Quote Post
Manfred
post 1.04.2010, 12:33:37
Post #4





Grupa: Zarejestrowani
Postów: 45
Pomógł: 0
Dołączył: 21.03.2005
Skąd: Olsztyn

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


@nospor
Tak, tak, przepraszam za błąd. Poprostu robiłem 100 kombinacji tego skryptu żeby znaleźć błąd, i zapomniałem poprawić. Ale poprawione też nie działa.

@Pawel_W
Cytat
i tak w ogóle to do zamiany ogonków wystarczy:

ten skrypt zamienia ąćęłńóśżź na a'cel'n'o'sz'z. Jak sie pozbyć tych ' między literami?
Go to the top of the page
+Quote Post
Pawel_W
post 1.04.2010, 13:57:13
Post #5





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


te ' to zmiękczenia głosek, trzeba by znowu str_replace zrobić ;p

chyba najlepsze rozwiązanie to
  1. $string = strtr($string, 'ĘÓĄŚŁŻŹĆŃęóąśłżźćń', 'EOASLZZCNeoaslzzcn');
Go to the top of the page
+Quote Post
Manfred
post 1.04.2010, 17:37:45
Post #6





Grupa: Zarejestrowani
Postów: 45
Pomógł: 0
Dołączył: 21.03.2005
Skąd: Olsztyn

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


@Pawel_W
Ten Twój ostatni kod to już w ogóle mi nie działa winksmiley.jpg

Ale chciałbym jeszcze raz się zapytać co jest tu nie tak (poprawiony kod):

p1.php
  1. <form action="poligon.php" method="post">
  2. <fieldset>
  3. <p><label>Tytuł ogłoszenia:</label><input type="text" class="text-long" name="post" /></p>
  4. </fieldset>
  5. </form>


poligon.php
  1. <?php
  2.  
  3. function ogonki_Zamiana($text){
  4. $text=str_replace('ę', 'e', $text );
  5. $text=str_replace('ó', 'o', $text );
  6. $text=str_replace('ą', 'a', $text );
  7. $text=str_replace('ś', 's', $text );
  8. $text=str_replace('ł', 'l', $text );
  9. $text=str_replace('ż', 'z', $text );
  10. $text=str_replace('ź', 'z', $text );
  11. $text=str_replace('ń', 'n', $text );
  12. $text=str_replace('ć', 'c', $text );
  13. $text=str_replace('!', '', $text );
  14. $text=str_replace('?', '', $text );
  15. $text=str_replace('*', '', $text );
  16. $text=str_replace('"', '', $text );
  17. $text=str_replace('<', '', $text );
  18. $text=str_replace('>', '', $text );
  19. $text=str_replace(')', '', $text );
  20. $text=str_replace('(', '', $text );
  21. $text=str_replace('!', '', $text );
  22. $text=str_replace('=', '', $text );
  23. $text=str_replace('+', '', $text );
  24. $text=str_replace('-', ' ', $text );
  25. $text=str_replace(';', '', $text );
  26. $text=str_replace(':', '', $text );
  27. $text=str_replace(',', '', $text );
  28. $text=str_replace('.', '', $text );
  29. $text=str_replace('_', '-', $text );
  30. $text=str_replace('{', '', $text );
  31. $text=str_replace('}', '', $text );
  32. $text=str_replace(']', '', $text );
  33. $text=str_replace('|', '', $text );
  34. return $text;}
  35.  
  36. $zmienna = "łść";
  37. $zmienna = ogonki_Zamiana($zmienna);
  38.  
  39. echo "Zwykla zmienna: " . $zmienna;
  40. echo "<br />";
  41. echo "<br />";
  42.  
  43. $post = $_POST['post'];
  44. $post = ogonki_Zamiana($post);
  45. echo "Post: " . $post;
  46.  
  47. ?>


efekt:

  1. Zwykla zmienna: lsc
  2.  
  3. Post: łść


$zmienna jest poprawnie zmieniona, ale $_POST['post'] już zostaje taka jaka była
Go to the top of the page
+Quote Post
Pawel_W
post 1.04.2010, 18:57:32
Post #7





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


hmm, w takim razie nie wiem co jest źle, strzelam że coś z kodowanie

tak btw. to nie wiesz, że str_replace jako argument przyjmuje również tablice? smile.gif
  1. $strings = array('ą', 'ć', 'ę');
  2. $replacements = array('a', 'c', 'e');
  3.  
  4. echo str_replace($strings, $replacements, 'ą ć ę');
Go to the top of the page
+Quote Post
Manfred
post 1.04.2010, 20:30:08
Post #8





Grupa: Zarejestrowani
Postów: 45
Pomógł: 0
Dołączył: 21.03.2005
Skąd: Olsztyn

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


@Pawel_W
Dzięki smile.gif Okazało się, że wszędzie trzeba było dać to samo kodowanie
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


Pozdrawiam i dziękuje za pomoc.
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: 19.07.2025 - 08:13