PHP truncates headers and is compatible with utf8 and gb2312 encodings

  • 2020-09-28 08:50:07
  • OfStack

 
<?php if(strlen($r[title])>45){$str=utf8Substr($r[title],0,15)."...";}else{$str=$r[title];}echo $str; ?> 
// The interception utf8 string  
function utf8Substr($str, $from, $len) 
{ 
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'. 
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s', 
'$1',$str); 
} 

Related articles: