【前言】
本文总结下tp中字符相关操作
【主体】
(1)限制输出显示的长度,截取显示前几个字符
这里显示0到10个字符,###表示本身
{$vol.content|msubstr=###,0,10}
例如:数据库中的字符过长时,会限制输出10个。后面为...
(2)去除前几个字符
php 在不知道字符串有多长的情况下,如何去除前三个字符?
$string='字符串'; $subject=substr_replace(string,'',0,3);
【总结】
最后附上修改封装后的截取字符串、去除空格等操作的函数
放到Common/Common/function.php下
$length) { if(function_exists("mb_substr")){ if($suffix) return mb_substr($str, $start, $length, $charset)."..."; else return mb_substr($str, $start, $length, $charset); }elseif(function_exists('iconv_substr')) { if($suffix) return iconv_substr($str,$start,$length,$charset)."..."; else return iconv_substr($str,$start,$length,$charset); } $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"; $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/"; $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/"; $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); if($suffix) return $slice."…"; return $slice; } else { return $str; } } /** * GET 请求 * 需要curl扩展支持 */ function http_get($url){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } } /** * POST 请求 * 需要curl扩展支持 */ function http_post($url,$param,$post_file=false){ $oCurl = curl_init(); if(stripos($url,"https://") !== FALSE){ curl_setopt($oCurl,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($oCurl,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($oCurl,CURLOPT_SSLVERSION,1); } if (is_string($param) || $post_file){ $strPOST = $param; } else { $aPOST = array(); foreach($param as $key => $val){ $aPOST[] = $key."=" . urlencode($val); } $strPOST = join("&",$aPOST); } curl_setopt($oCurl,CURLOPT_URL,$url); curl_setopt($oCurl,CURLOPT_RETURNTRANSFER,1); curl_setopt($oCurl,CURLOPT_POST,true); curl_setopt($oCurl,CURLOPT_POSTFIELDS,$strPOST); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"]) == 200){ return $sContent; }else{ return false; } } /** * 空格换行符过滤 */ function trimAll($parma){ if(is_array($parma)){ return array_map('trimAll',$parma); } $before = array(" "," ","\t","\r","\n"); $after = array('','','','',''); return str_replace($before,$after,$parma); } // 评论显示替换表情标签 function reFace($str){ for($i=1;$i<76;$i++){ $str = str_replace("[em_$i]","",$str); } return $str; }
.