Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]str_replace - skrócenie
bahh
post
Post #1





Grupa: Zarejestrowani
Postów: 261
Pomógł: 2
Dołączył: 13.05.2012

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


Witam, chciałbym skrócić ten kod do jednej linijki

  1. $_POST["link"] = str_replace('!', '', $_POST["link"]);
  2. $_POST["link"] = str_replace('@', '', $_POST["link"]);
  3. $_POST["link"] = str_replace('#', '', $_POST["link"]);
  4. $_POST["link"] = str_replace('$', '', $_POST["link"]);
  5. $_POST["link"] = str_replace('%', '', $_POST["link"]);
  6. $_POST["link"] = str_replace('&', '', $_POST["link"]);
  7. $_POST["link"] = str_replace('*', '', $_POST["link"]);
  8. $_POST["link"] = str_replace('(', '', $_POST["link"]);
  9. $_POST["link"] = str_replace(')', '', $_POST["link"]);
  10. $_POST["link"] = str_replace('~', '', $_POST["link"]);
  11. $_POST["link"] = str_replace('`', '', $_POST["link"]);
  12. $_POST["link"] = str_replace('+', '', $_POST["link"]);
  13. $_POST["link"] = str_replace('=', '', $_POST["link"]);
  14. $_POST["link"] = str_replace('[', '', $_POST["link"]);
  15. $_POST["link"] = str_replace(']', '', $_POST["link"]);
  16. $_POST["link"] = str_replace('|', '', $_POST["link"]);
  17. $_POST["link"] = str_replace(';', '', $_POST["link"]);
  18. $_POST["link"] = str_replace(':', '', $_POST["link"]);
  19. $_POST["link"] = str_replace('"', '', $_POST["link"]);
  20. $_POST["link"] = str_replace('/', '', $_POST["link"]);
  21. $_POST["link"] = str_replace(',', '', $_POST["link"]);
  22. $_POST["link"] = str_replace('.', '', $_POST["link"]);
  23. $_POST["link"] = str_replace('?', '', $_POST["link"]);
  24. $_POST["link"] = str_replace('/', '', $_POST["link"]);
  25. $_POST["link"] = str_replace('<', '', $_POST["link"]);
  26. $_POST["link"] = str_replace('>', '', $_POST["link"]);
  27. $_POST["link"] = str_replace('_', '', $_POST["link"]);
  28. $_POST["link"] = str_replace('^', '', $_POST["link"]);
  29. $_POST["link"] = str_replace('\\', '', $_POST["link"]);


wystarczy zastąpić to takim kodem?

  1. $_POST["link"] = str_replace('^@#$%&*()~`+=[]|;:""/,.?/<>_^\\', '', $_POST["link"]);


chodzi mi o usunięcie wszystkich znaków specjalnych z wyjątkiem spacji, - oraz liter wraz cyframi
Go to the top of the page
+Quote Post
devbazy
post
Post #2





Grupa: Zarejestrowani
Postów: 93
Pomógł: 4
Dołączył: 10.08.2009

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


Testowałeś chociaż ten kod, że o to pytasz?
Go to the top of the page
+Quote Post
Greg0
post
Post #3





Grupa: Zarejestrowani
Postów: 264
Pomógł: 54
Dołączył: 31.08.2007
Skąd: Tychy

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


Możesz jako pierwszy parametr str_replace dać tablicę z tymi znakami czyli array('^', '#' .... ), a na reszcie to co masz. Lub użyć preg_replace()
Go to the top of the page
+Quote Post
Michael2318
post
Post #4





Grupa: Zarejestrowani
Postów: 651
Pomógł: 116
Dołączył: 3.06.2012
Skąd: Lędziny

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


  1. $array_r = array('!', ',', '@', '_', '-');
  2. $array_rr = array('', '', '', '', '');
  3.  
  4. $_POST["link"] = str_replace($array_r, $array_rr, $_POST["link"]);
Go to the top of the page
+Quote Post
AdIoS_Neo
post
Post #5





Grupa: Zarejestrowani
Postów: 159
Pomógł: 43
Dołączył: 8.03.2009

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


Witam,
według mnie tak będzie najzwięźlej i najoptymalniej:
  1. preg_replace("/[^\sa-z0-9-]/i", '', $_POST["link"]);


Ten post edytował AdIoS_Neo 29.03.2013, 15:13:47
Go to the top of the page
+Quote Post
bahh
post
Post #6





Grupa: Zarejestrowani
Postów: 261
Pomógł: 2
Dołączył: 13.05.2012

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


pytanie czy polskie znaki zostaną czy nie?
Go to the top of the page
+Quote Post
AdIoS_Neo
post
Post #7





Grupa: Zarejestrowani
Postów: 159
Pomógł: 43
Dołączył: 8.03.2009

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


w takim przypadku:
  1. preg_replace("/[^\sa-ż0-9-]/i", '', $_POST["link"]);

tak (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

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: 25.08.2025 - 19:44