完美解决All in One SEO Pack对中文支持不友好的问题

完美解决All in One SEO Pack对中文支持不友好的问题

Jonty
2015-01-05 / 2 评论 / 77 阅读 / 正在检测是否收录...

大家都知道All in One SEO Pack是wordpress的一个搜索引擎优化的插件,但它对中文支持不友好。修改它的代码可以完美支持中文。
All in One SEO对英文支持很好的。因为它只考虑的单字节语言的情况,没有考虑多字节(例如中文等非英文的语言)的情况。

问题介绍

All in One SEO Pack只截取英文空格(" ")之前的内容作为description(描述)的内容,但是我们写中文博客,多数不会在文章中出现英文空格,更别说在开头的恰当位置了,所以中文博客用它就就悲剧了,只生成很短的描述,甚至不生成描述。

解决方法(最新版)

在{wordpress目录}/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php文件中搜索function trim_excerpt_without_filters方法,类似这样的:

```css

function trim_excerpt_without_filters( $text, $max = 0 ) {
$text = str_replace( ']]>', ']]>', $text );
$text = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $text );
$text = wp_strip_all_tags( $text );
if ( !$max ) $max = $this->maximum_description_length;
$len = $this->strlen( $text );
if ( $max < $len ) {
if ( function_exists( 'mb_strrpos' ) ) {
$pos = mb_strrpos( $text, ' ', -($len - $max) );
if ( $pos === false ) $pos = $max;
if ( $pos > $this->minimum_description_length ) {
$max = $pos;
} else {
$max = $this->minimum_description_length;
}
} else {
while( $text[$max] != ' ' && $max > $this->minimum_description_length ) {
$max--;
}
}
}
$text = $this->substr( $text, 0, $max );
return trim( stripslashes( $text ) );
}
```

使用下面的代码替换掉上面的代码即可。

```css

function trim_excerpt_without_filters( $text, $max = 0 ) {
$text = str_replace( ']]>', ']]&gt;', $text );
$text = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $text );
$text = wp_strip_all_tags( $text );
if ( !$max ) $max = $this->maximum_description_length;
$len = $this->strlen( $text );
if ( $max < $len ) {
if ( function_exists( 'mb_strrpos' ) ) {
$pos = mb_strrpos( $text, ' ', -($len - $max) );
if ( $pos === false ) $pos = $max;
if ( $pos > $this->minimum_description_length ) {
$max = $pos;
} else {
$max = $this->minimum_description_length;
}
} else {
while((ord($text[$max]) & 0x80) != 0 && (ord($text[$max]) & 0x40) == 0
&& $max > $this->minimum_description_length ) {
$max--;
}
}
}
$text = $this->substr( $text, 0, $max );
return trim( stripslashes( $text ) );
}
```

解决原理

wordpress的博客多数都是UTF8格式的,那么我们只需要研究下UTF8格式就好了,当然如果使用gbk或者跟utf8不兼容的格式,那么这个方法是不可行的。

UTF-8是一种变长的编码方式。它可以使用1~4个字节表示一个符号(字),根据不同的符号而变化字节长度。
UTF-8的编码规则很简单,只有二条:

  1. 对于单字节的符号,字节的第一位设为0,后面7位为这个符号的unicode码。因此对于英语字母,UTF-8编码和ASCII码是相同的。
  2. 对于n字节的符号(n>1),第一个字节的前n位都设为1,第n+1位设为0,后面字节的前两位一律设为10。剩下的没有提及的二进制位,全部为这个符号的unicode码。

下表总结了编码规则,字母x表示可用编码的位。

```css

Unicode符号范围 | UTF-8编码方式
(十六进制) | (二进制)
--------------------+---------------------------------------------
0000 0000-0000 007F | 0xxxxxxx
0000 0080-0000 07FF | 110xxxxx 10xxxxxx
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

```
那么我们只要以一个字的第一个字节处结束(不包含该字节)就能获取完整的字符串了,也不会有乱码产生。
从上面的编码规则可以看出,如果一个字节的最高位为0,或者前两个高位都为1,那么这个字节一定是一个字的第一个字节。

本文共 542 个字数,平均阅读时长 ≈ 2分钟
0

打赏

海报

正在生成.....

评论 (2)

取消
  1. 头像
    神父 Lv.3
    中国陕西省 ·Windows 7 · Google Chrome
    沙发

    特么高深的技术

    回复 删除 垃圾
    1. 头像
      gkroot Lv.6
      中国河南省 ·iPhone · UC Browser
      @ 神父

      @神父 ....作者牛逼

      回复 删除 垃圾