WordPress内置函数mb_strimwidth()不能用

2019年12月22日13:22:28 发表评论 热度137 ℃
  1. <?php echo mb_strimwidth(strip_tags($post->post_content),0,100,'...');?>

通常,通过上面的函数,即可截取WordPress的文章里首段文字了,可是,有些服务器或主机尽然不支持 php-mbstring ,无奈啊。
如果是自己的服务器,可以尝试用 yum install php-mbstring 来解决问题
但是,如果是虚拟主机,没有什么权限的话,我们重新定义一个截取字符串的函数方法即可,代码如下:

  1. //自定义截断函数
  2. function dm_strimwidth($str ,$start , $width ,$trimmarker ){
  3. $output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$width.'}).*/s','\1',$str);
  4. return $output.$trimmarker;
  5. }

将以上代码放入到主题目录的functions.php内
然后对以下代码

  1. <?php echo mb_strimwidth(strip_tags($post->post_content),0,100,'...');?>

进行改造,如下

  1. <?php echo dm_strimwidth(strip_tags($post->post_content),0,100,'...');?>

嗯,说白了,就是将mb换成了dm,嗯,完事,再见。。。

瓜皮猪

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: