sina微博输入字数统计,包含链接的字数统计处理

By | 2012 年 9 月 14 日

做微博发送,sina微博接口,带连接的字数统计功能,度来的,分享给大家!

PS:修改一下,亦能搞定腾讯哦!!

某强人从新浪弄下来的js方法,本人测试有效+好用!!!!!

<script type="text/javascript">
var getLength = (function() {
var trim = function(h) {alert(h);
try {
return h.replace(/^\s+|\s+$/g, "");
} catch (j) {
return h;
}
};
var byteLength = function(b) {
if (typeof b == "undefined") {
return 0;
}
var a = b.match(/[^\x00-\x80]/g);
return (b.length + (!a ? 0 : a.length));
};
return function(q, g) {
g = g || {};
g.max = g.max || 140;
g.min = g.min || 41;
g.surl = g.surl || 20;
var p = trim(q).length;
if (p > 0) {
var j = g.min, s = g.max, b = g.surl, n = q;
var r = q.match(/(http|https):\/\/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+([-A-Z0-9a-z\$\.\+\!\_\*\(\)\/\,\:;@&=\?~#%]*)*/gi)
|| [];
var h = 0;
for ( var m = 0, p = r.length; m < p; m++) {
var o = byteLength(r[m]);
if (/^(http:\/\/t.cn)/.test(r[m])) {
continue;
} else {
if (/^(http:\/\/)+(weibo.com|weibo.cn)/.test(r[m])) {
h += o <= j ? o : (o <= s ? b : (o - s + b));
} else {
h += o <= s ? b : (o - s + b);
}
}
n = n.replace(r[m], "");
}
return Math.ceil((h + byteLength(n)) / 2);
} else {
return 0;
}
};
})();
//调用测试
var fff = "汉字啊啊啊d";
alert(getLength(fff));
</script>

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据