Taken micro-channel should be noted that the nickname, emoticons and special characters, if you do not have a regular filter out, use slice (0,1) is directly first string is not taken, because the emoticon occupies 2 bytes. interception half, ios will complain, android may be garbled, these are not what we want!
ES6 provided to us in a way Array.from ()
Array.from () method is to convert an array of objects of a class comparable cases or objects into a real array.
Class array of objects: The most basic requirement is that the object has a length property.
Used here it is: convert a string to an array of this method
let str = "Hello";
console.log(Arry.from(str)); // ["H","e","l","l","o"];
formatName(value, num) {
if (!value) return ''
value = value.replace(/(^\s+)|(\s+$)/g, "").replace(/\'/g,"‘").replace(/\"/g,"“");
var s = '';
var strLength = this.getStrLength(value);
for (var i = 0; i < strLength - 1; i++) {
if(i>=6) break;
s += '*';
}
return this.truncated(value, num) + s;
},
truncated(str, num){
return Array.from(str).slice(0, num).join('');
},
getStrLength(str){
return Array.from(str).length;
},
Extending a form submission time, the filter expression method emoji
js检测输入框是否含有emoji表情(移动端比较常见)
var param = document.getElementById(id).value;
var regRule = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
if(param.match(regRule)) {
param = param.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, “”);
alert(“不支持表情”);
}
PHP后端代码
function have_emoji($str){
$mat = [];
preg_match_all('/./u', $str,$mat);
foreach ($mat[0] as $v){
if(strlen($v) > 3){return true;}
}
return false;
}