本文整理自网络,侵删。
本文实例讲述了WordPress中文截取函数用法。分享给大家供大家参考。具体分析如下:
很多朋友直接使用php substr函数截取中文是会出现乱码,下面我先来给各位总结中文字符截取的函数,然后以wordpress截取中文标题为例给大家介绍实例应用.
中文截取函数,代码如下:
代码如下:
function utf8_trim($str)
{
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
function mul_excerpt ($excerpt,$excerpt_length) {
$myexcerpt = substr($excerpt,0,$excerpt_length);
return utf8_trim($myexcerpt) . '… ';
}
或者这样也可以
{
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
function mul_excerpt ($excerpt,$excerpt_length) {
$myexcerpt = substr($excerpt,0,$excerpt_length);
return utf8_trim($myexcerpt) . '… ';
}
或者这样也可以
代码如下:
//自动截断不乱码
// jieduan luanma
/////////////////
function Limit_Char($max_char = 200, $more_text = '...', $more_link_text = '', $limit_type = 'content') {
if ($limit_type == 'title') { $limiter = get_the_title(); }
else { $limiter = get_the_content(); }
$limiter = apply_filters('the_content', $limiter);
$limiter = strip_tags(str_replace(']]>', ']]>', $limiter));
if (strlen($limiter) > $max_char) {
$limiter = substr($limiter, 0, $max_char+1);
$limiter = utf8_conver($limiter);
echo $limiter;
echo $more_text;
if ($more_link_text != '') {
echo ' '.$more_link_text.'';
}
} else {
echo $limiter;
}
}
function utf8_conver($str) {
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
截取中文标题,代码如下:
// jieduan luanma
/////////////////
function Limit_Char($max_char = 200, $more_text = '...', $more_link_text = '', $limit_type = 'content') {
if ($limit_type == 'title') { $limiter = get_the_title(); }
else { $limiter = get_the_content(); }
$limiter = apply_filters('the_content', $limiter);
$limiter = strip_tags(str_replace(']]>', ']]>', $limiter));
if (strlen($limiter) > $max_char) {
$limiter = substr($limiter, 0, $max_char+1);
$limiter = utf8_conver($limiter);
echo $limiter;
echo $more_text;
if ($more_link_text != '') {
echo ' '.$more_link_text.'';
}
} else {
echo $limiter;
}
}
function utf8_conver($str) {
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
截取中文标题,代码如下:
代码如下:
echo '</p><p>'. mb_substr(get_the_title(),0,18,"utf8")."..." .'</p></li>';
相关阅读 >>
wysiwyg web builder 17激活教程 附汉化步骤
wordpress导入数据库出现”unknown collation ‘utf8mb4_unicode_ci”错误的解决办法
centos下搭建php环境与wordpress博客程序的全流程总结
bluehost服务器主机购买及安装wordpress的教程
更多相关阅读请进入《wordpress》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。