web开发中前、后端常用数据处理方法整理(集各家之言)-PHP源码
1. [代码][PHP]代码
------------------------- 前端 /** * 前端公共函数方法整理 * @author Weige * 备注: 配合jqury.js 使用 * 2012-04 */ //获取字符串长度 function getWordSize(str) { if(!str) return null; var length = 0; var str = str; var regDoub = /[^x00-xff]/g; var regSingl = /[x00-xff]/g var douL = str.match(regDoub); var singL = str.match(regSingl) if(douL){ length+=douL.length*2; } if(singL){ length+=singL.length; } length/=2; length = Math.ceil(length); return length; } //祛除前后空格 function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ''); } //中文汉字编码判断 function isChinese(str) { var str = str.replace(/(^\s*)|(\s*$)/g,''); if (!(/^[\u4E00-\uFA29]*$/.test(str) && (!/^[\uE7C7-\uE7F3]*$/.test(str)))) { return false; } return true; } //手机判断 function isMobile(str) { if(/^1[345689]\d{9}/.test(str)) { return true; } return false; } // oschina //特殊字符转换 function htmlEncode(str) { var s = ""; if (str.length == 0) return ""; //s = s.replace(/ /g, " "); s = str.replace(/&/g, "&"); s = s.replace(//g, ">"); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, "
"); return s; } function htmlDecode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/>/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); s = s.replace(/
/g, "\n"); return s; } //使用ajax提交数据 function ajaxPost(the_url,the_param,succ_callback) { $.ajax({ type : 'POST', cache : false, url : the_url, data : the_param, success : succ_callback }); } //使用ajax获取数据 function ajaxGet(the_url,succ_callback,error_callback) { $.ajax({ type : 'GET', cache : true, url : the_url, success : succ_callback, error : error_callback }); } //发送json格式数据 function ajaxPostJson(the_url,data_pro,succ_callback,error_callback) { $.ajax({ async : false,//同步更新 type : 'post', dataType : 'json', data : data_pro, url : the_url, success : succ_callback, error : error_callback }); } function real_len(name) { return (name.replace(/[^\x00-\xff]/g,"**").length); } function is_email(email) { var reg=/^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/; return reg.test(email); } function is_number(name) { var reg = /^\d+$/g; return reg.test(name); } function is_pwd(name) { var reg = /^[A-Za-z@0-9_-]+$/g; return reg.test(name); } ---------------------------------后端 = 0; $t-- ) { $a = floor( $id / pow( $base, $t ) ); $out = $out . substr( $index, $a, 1 ); $id = $id - ( $a * pow( $base, $t ) ); } return $out; } /** * 短链接密钥 * */ function url_key($url,$key="wei爱微博") { $x = sprintf("%u", crc32($key.$url)); $show = ''; while($x> 0) { $s = $x% 62; if($s> 35) $s = chr($s+61); elseif($s> 9 && $s<=35) $s = chr($s+ 55); $show.= $s; $x = floor($x/62); } return $show; } /** * 标签key * */ function tag_key($tag,$key="wei爱话题") { $tag = str_replace(" ", "", $tag); $tag.= $key; $hash = md5($tag); $hash = $hash[0] | ($hash[1] <<8 ) | ($hash[2] <<16) | ($hash[3] <<24) | ($hash[4] <<32) | ($hash[5] <<40) | ($hash[6] <<48) | ($hash[7] <<56); return $hash % 99999999; } /** * 过滤非法标签 * */ function strip_selected_tags($str,$disallowable="《script》