Strikeout all repeated words using jquery, javascript or PHP -


is there easy way strikeout repeated words (excluding first one) in string using jquery/javascript or php?

ex (using javascript):

var text = 'you know that. know that. might know';  strikeoutrepeatedfunction(text); 

result: know that. know . might know .

thanks

you can this,

  1. split string using split(' '), split space.
  2. now iterate on them using map() , store unique words in array
  3. in case of repetition return string wrapped del tag else return string itself.
  4. to avoid problem case convert string lowercase while comparing , use tolowercase() that.
  5. combine array using join(' ')

var text = 'you know that. know that. might know';    function strikeoutrepeatedfunction(t) {    var words = [];    return t.split(' ').map(function(v) {      if (words.indexof(v.tolowercase()) === -1)        words.push(v.tolowercase());      else        return '<del>' + v + '</del>';      return v;      }).join(' ');  }    document.write(strikeoutrepeatedfunction(text));

sometimes . in string may cause problem avoid use

var text = 'you know that.  know that. might know. you';      function strikeoutrepeatedfunction(t) {    var words = [];    return t.split(' ').map(function(v) {      if (words.indexof(v.replace(/^\.+|\.+$/,'').tolowercase()) === -1)        words.push(v.replace(/^\.+|\.+$/,'').tolowercase());      else        return '<del>' + v + '</del>';      return v;      }).join(' ');  }    document.write(strikeoutrepeatedfunction(text));


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -