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,
- split string using
split(' '), split space. - now iterate on them using
map(), store unique words in array - in case of repetition return string wrapped
deltag else return string itself. - to avoid problem case convert string lowercase while comparing , use
tolowercase()that. - 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
Post a Comment