Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] str_replace() nie działa w $_POST
Forum PHP.pl > Forum > Przedszkole
Manfred
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. ?>
nospor
nie: echo "Post: " . $_POST['post'];
a: echo "Post: " . $post;

mysl,mysl,mysl.... winksmiley.jpg
Pawel_W
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;
Manfred
@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?
Pawel_W
te ' to zmiękczenia głosek, trzeba by znowu str_replace zrobić ;p

chyba najlepsze rozwi±zanie to
  1. $string = strtr($string, 'ĘÓˇ¦ŁŻ¬ĆŃęó±¶łżĽćń', 'EOASLZZCNeoaslzzcn');
Manfred
@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
Pawel_W
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, '± ć ę');
Manfred
@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.
To jest wersja lo-fi głównej zawarto¶ci. Aby zobaczyć pełn± wersję z większ± zawarto¶ci±, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.