freddyw Posted May 1, 2010 Share Posted May 1, 2010 So basically im trying to create a function that checks lines entered into a text box that removes duplicates completely. For example in the text box this is wrote. The And To What Where The One And Where I dont want duplicates changing to one. I want them out. So it reads To What One Just a starting point would help please. Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 Does it matter what order they're in? Can it be: What, One, To (in that order)? Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/#findComment-1051752 Share on other sites More sharing options...
freddyw Posted May 2, 2010 Author Share Posted May 2, 2010 Thanks for the reply. The order isnt important at all Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/#findComment-1052088 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 Well, here's a function that takes an array of words and returns non-duplicates to your specifications. function noDupes (arr) { var tmp = new Array(), results = new Array(); for (var m = arr.length - 1; m > -1; m--) { if (tmp[arr[m]] === undefined) tmp[arr[m]] = 1; else tmp[arr[m]] = undefined; } for (var t in tmp) { if (t !== undefined) results.push(t); } return results; } var e = noDupes(new Array("the", "and", "to", "where", "and", "the")); Your job is to create the array. I won't help with that. Seems trivial enough. Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/#findComment-1052097 Share on other sites More sharing options...
satya61229 Posted May 3, 2010 Share Posted May 3, 2010 I hope that function above will work. Then you can use the split() method of Js. This should work for creating array from text box - var arr1 = document.getElementById('txt1').split(" "); Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/#findComment-1052412 Share on other sites More sharing options...
freddyw Posted May 4, 2010 Author Share Posted May 4, 2010 Brilliant! Thanks guys Appreciate the help Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/#findComment-1053076 Share on other sites More sharing options...
freddyw Posted May 7, 2010 Author Share Posted May 7, 2010 Im coming a bit stuck I now have this... <html> <head> <title>Reomove Dupes</title></head> <body> <form onsubmit="removeDups(); return false;"> <textarea rows="30" cols="100" id="list"> </textarea> <br> <input type="submit" value="Submit" /> </form> <script type="text/javascript"> function noDupes (arr) { var tmp = new Array(), results = new Array(); for (var m = arr.length - 1; m > -1; m--) { if (tmp[arr[m]] === undefined) tmp[arr[m]] = 1; else tmp[arr[m]] = undefined; } for (var t in tmp) { if (t !== undefined) results.push(t); } return results; } function removeDups() { var txtbox = document.getElementById("list"); txtbox.value = noDupes(txtbox.value.split(/\n/)).join("\n"); } </script> </body> </html> if i type the and and im left with (after clicking submit twice) the and I want to completely remove what was duplicated meaning the only word left in the box would be 'the' Any ideas? please? Link to comment https://forums.phpfreaks.com/topic/200375-removing-text/#findComment-1054714 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.