
Added by roScripts on August-25-2007, 2:42 pm
/*
************************************************************
Developed by R.Arul Kumaran [arul@shockwave-india.com] *
for more code keep visiting [www.shockwave-india.com/blog] *
************************************************************
*/
//getWords returns Array of words and non words from a string
//gets the words in to array elements
_global.getWords = function(txt) {
var Arr = new Array();
var lastIndex = 0;
for (var i = 0; i<txt.length; i++) {
var code = txt.charCodeAt(i);
var char = txt.charAt(i);
if (isWord(code)) {
//trace(" '"+char+"' "+"isWord");
} else {
//trace(" '"+char+"' "+"is Not Word");
var word = txt.substring(lastIndex, i);
if (word == '') {
/*
empty string that means
we have 2 or more non word chars together
so add them together
*/
if (Arr.length != 0) {
Arr[Arr.length-1] = Arr[Arr.length-1]+char;
} else {
Arr.push('');
Arr.push(char);
}
} else {
Arr.push(word);
Arr.push(char);
}
lastIndex = i+1;
}
}
if (lastIndex != txt.length) {
//last char is not a non-word so add the lost word to list
word = txt.substring(lastIndex, txt.length);
Arr.push(word);
Arr.push('');
}
trace(Arr.length);
return Arr;
}
_global.isWord = function(c) {
/*
Charcode range for non word chars
9,10,13
32 to 47
58 to 64
91 to 96
123 to 126
*/
//trace(c);
if (c == 10 || c == 9 || c == 13 || (c>=32 && c<=44) || (c>=46 && c<=47) ||
(c>=58 && c<=64) || (c>=91 && c<=96) || (c>=123 && c<=126)) {
return false;
} else {
return true;
}
}
//HTML Encodes the char
_global.htmlEncode = function(chr) {
switch (chr) {
case ' ' :
chr = " ";
break;
case '<' :
chr = "<";
break;
case '>' :
chr = ">";
break;
case '&' :
chr = "&";
break;
case '\"' :
chr = """;
break;
case "'" :
chr = "'";
break;
}
return chr;
}
/*
Usage:-
txt="Huh! Multimedia?"
//get the words array
wordsArr=getWords(txt);
//wordsArr now contatins ["Huh", "! ", "Multimedia", "?"]
//in which first element is the word and second is the nonword and so on
*/
Added by roScripts on March-26-2008, 5:14 pm
Added by roScripts on March-26-2008, 5:13 pm
Added by roScripts on March-26-2008, 5:09 pm
Added by roScripts on March-26-2008, 5:08 pm
Added by roScripts on March-26-2008, 5:07 pm
