1

Hello good friends :)

I have a question about jquery. Is there a function in jquery to replace the many words that have been mapped within an object or array.

Example in php:

<?php 
    $textBefore  = "PHP is amazing open source programmer language, 
                    there many webs around the world that are built using php";
    $forReplace  = array("amazing","programmer","webs");
    $replacement = array("powerfull","programming","websites");
    $textAfter   = str_replace( $forReplace , $replacement , $textBefore);
    echo $textAfter;
?>

it will be produce

PHP is powerfull open source programming language, 
there many websites around the world that are built using php

How do I do a similar thing in jquery? I did this to reduce the performance of the server so I thought to do this work by the browser. Because the contents fr replace in one show page is too much.

Thanks in advance.

4 Answers 4

2
var value = $("#text").val(); // value = 9.61 use $("#text").text() if you are not on select box...
value = value.replace(".", ":"); // value = 9:61
// can then use it as
$("#anothertext").val(value);
2
  • these isn't an array though, you could probably fix it with jquery.each() Commented Sep 3, 2014 at 8:08
  • I already know that but what I need is a function to simultaneously run all of the turn. Does that mean I have to do it over and over again without mapping the object? Is this the last? Commented Sep 3, 2014 at 8:09
1

You don't need jQuery, just use Javascript's replace method.

var text  = 'PHP is amazing open source programmer language, there many webs around the world that are built using php';

text = text.replace('amazing', 'powerful');
text = text.replace('programmer', 'programming');
text = text.replace('webs', 'websites');

You can replicate PHP's str_replace method like so:

function str_replace(search, replace, subject) {
    for (i=0; i<replace.length; i++) {
        subject = subject.replace(search[i], replace[i]);
    }
    return subject;
}

var subject = "This is a test string.";
var search = ['test', 'string'];
var replace = ['cool', 'thang'];

subject = str_replace(search, replace, subject);

\\ subject is now 'This is a cool thang';
2
  • I already know that but what I need is a function to simultaneously run all of the turn. Does that mean I have to do it over and over again without mapping the object? Is this the last? Commented Sep 3, 2014 at 8:10
  • I added code to show you how to replicate PHP's str_replace method. Commented Sep 3, 2014 at 8:19
1

You can achieve this with one for cycle.

var text = "This is some sample text.";
var target = ['some', 'text'];
var replace = ['replaced', 'sentence'];

for(var i = 0; i < replace.length; i++){
    text = text.replace(target[i], replace[i])
}

Demo

0
1

Refer phpjs.org which has javascript implementations of php functions.

The code for str_replace is given below. Source : http://phpjs.org/functions/str_replace/

function str_replace(search, replace, subject, count) {
  var i = 0,
    j = 0,
    temp = '',
    repl = '',
    sl = 0,
    fl = 0,
    f = [].concat(search),
    r = [].concat(replace),
    s = subject,
    ra = Object.prototype.toString.call(r) === '[object Array]',
    sa = Object.prototype.toString.call(s) === '[object Array]';
  s = [].concat(s);
  if (count) {
   this.window[count] = 0;
  }

  for (i = 0, sl = s.length; i < sl; i++) {
    if (s[i] === '') {
      continue;
    }
    for (j = 0, fl = f.length; j < fl; j++) {
      temp = s[i] + '';
      repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
      s[i] = (temp)
    .split(f[j])
    .join(repl);
      if (count && s[i] !== temp) {
        this.window[count] += (temp.length - s[i].length) / f[j].length;
      }
    }
  }
  return sa ? s : s[0];
}

Usage:

$textBefore  = "PHP is amazing open source programmer language,there many webs around the world that are built using php";
$forReplace  = ["amazing","programmer","webs"];
$replacement = ["powerfull","programming","websites"];
$textAfter   = str_replace( $forReplace , $replacement , $textBefore);

$textAfter will have PHP is powerfull open source programming language,there many websites around the world that are built using php

2
  • Please try to avoid link only answers rather add relevant code. Commented Sep 3, 2014 at 8:27
  • @hinata: had some issues with formatting while i was copying and pasting the code. so just gave the link. Will keep this in mind from now on. Commented Sep 3, 2014 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.