function substr_count( haystack, needle, offset, length ) { // Count the number of substring occurrences
//
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
var pos = 0, cnt = 0;
if(isNaN(offset)) offset = 0;
if(isNaN(length)) length = 0;
offset--;
while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
if(length > 0 && (offset+needle.length) > length){
return false;
} else{
cnt++;
}
}
return cnt;
}