如何使用php从mysql获取unicode字符的子串

时间:2013-04-05 12:16:50

标签: php mysql sql

Unicode字符以这种格式存储在mysql数据库中

یہاں تو

我的数据库中不仅有unicode字符,而且混合了html和英文字符。

问题是我想从数据库字段'post_body'获取字符串的一部分 我使用了以下sql查询

"SELECT SUBSTRING(post_body,1,120) as pst_body from mytable";

这个字符串准确地给了我120个字符。但问题是如果数据库中有unicode符号ی is equal to 1 unicode character,那么我的要求就不会以这种方式实现。 是否有任何函数可以返回我指定数量的字符,无论是unicode字符还是英文字符,意味着如果有unicode数据,它应该将ی计为一个字符。

1 个答案:

答案 0 :(得分:0)

我不认为,mysql中有任何选项,你可以从mysql中获取数据,然后在PHP中执行操作。

function getSubstring($string, $number){

    $keywords = preg_split("/([&])+/", htmlentities($string));
    $finalArray = array();
    unset($keywords[0]);

    for($index = 1;$index <= $number;$index++){
        $finalArray[] = $keywords[$index];
    }
    return str_replace('amp;', '&', implode('', $finalArray));
}

 //$string = &#1740;&#1729;&#1575;&#1722; &#1578;&#1608;
 //$number = 10;// number of character to be fetch
 echo getSubstring($string,10);