Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript] Funkcja str_replace w javascript ?
Forum PHP.pl > Forum > Przedszkole
kkuubbaa88
witam

poszukuje funkcji z php "str_replace", ktora bedzie dzialala pod javascriptem. Chcialbym podac tablice znakow zrodlowych, potem druga tablice znakow na jaka zamienić i za pomoca funkcji podmienic to w javascriptcie. Jak sie taka funkcja nazywa ?
wookieb
http://phpjs.org/functions/str_replace:527
kkuubbaa88
czyli taka sama w php jest w js ? tak samo sie jej uzywa ?
wookieb
Oświecę cię Jebitnym napisem z podanego linku "Use PHP functions in JavaScript"
kkuubbaa88
zrobilem takie cos, ale nie chce dzialac.


var tablica_znakow = new Array(' ','A');
var tablica_znakow2 = new Array('_','a');

wartosc_input = str_replace(tablica_znakow, tablica_znakow2, wartosc_input);

alert(wartosc_input);


co jest nie tak ?
Pawel_W
omg, w js do tego jest funkcja replace, np.
  1. var str = "Ala ma kota";
  2. var str = str.replace(/Ala/gi, "Ola");
  3. document.write(str);
wookieb
Cytat(Pawel_W @ 3.01.2010, 18:17:22 ) *
omg, w js do tego jest funkcja replace, np.

Najpierw CZYTAJ potem gadaj.

1) A funkcję str_replace masz w kodzie?
2) U mnie wszystko działa
3) Wydaje mi się, że nie masz za bardzo pojęcia co robisz.

[JAVASCRIPT] pobierz, plaintext
  1. function str_replace (search, replace, subject, count) {
  2. // Replaces all occurrences of search in haystack with replace
  3. //
  4. // version: 909.322
  5. // discuss at: <a href="http://phpjs.org/functions/str_replace" target="_blank">http://phpjs.org/functions/str_replace</a> // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  6. // + improved by: Gabriel Paderni
  7. // + improved by: Philip Peterson
  8. // + improved by: Simon Willison (http://simonwillison.net)
  9. // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + bugfixed by: Anton Ongson
  10. // + input by: Onno Marsman
  11. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  12. // + tweaked by: Onno Marsman
  13. // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  14. // + input by: Oleg Eremeev
  15. // + improved by: Brett Zamir (http://brett-zamir.me)
  16. // + bugfixed by: Oleg Eremeev
  17. // % note 1: The count parameter must be passed as a string in order // % note 1: to find a global variable in which the result will be given
  18. // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
  19. // * returns 1: 'Kevin.van.Zonneveld'
  20. // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
  21. // * returns 2: 'hemmo, mars' var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
  22. f = [].concat(search),
  23. r = [].concat(replace),
  24. s = subject,
  25. ra = r instanceof Array, sa = s instanceof Array; s = [].concat(s);
  26. if (count) {
  27. this.window[count] = 0;
  28. }
  29. for (i=0, sl=s.length; i < sl; i++) {
  30. if (s[i] === '') {
  31. continue;
  32. }
  33. for (j=0, fl=f.length; j < fl; j++) { temp = s[i]+'';
  34. repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
  35. s[i] = (temp).split(f[j]).join(repl);
  36. if (count && s[i] !== temp) {
  37. this.window[count] += (temp.length-s[i].length)/f[j].length;} }
  38. }
  39. return sa ? s : s[0];
  40. }
  41.  
  42. var tablica_znakow = new Array(' ','A');
  43. var tablica_znakow2 = new Array('_','a');
  44.  
  45. wartosc_input ='Ala ma kota';
  46. wartosc_input = str_replace(tablica_znakow, tablica_znakow2, wartosc_input);
  47.  
[JAVASCRIPT] pobierz, plaintext
cojack
Ale na prawdę po jakiego czorta pisać str_replace jak jest .replace() w js? Ino dana zmienna musi być stringiem.
wookieb
Pewnie nie, Ale chciał podać tablicę to ma tablicę.
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.