PHP GD庫 生成圖片時,對於英文的換行

前言

最近開發遇到後端生成圖片時英文的換行

貼代碼

/*
 * 字符串相關處理
 */
class StringUtils
{
    /**
     * 英文字符串換行
     * 如果是字母,並且到了換行的地方,則需要看這個字符的下一個字符是否是字母,如果是的話就需要回到上一個不是字母的地方
     * 注意:中文和英文
     */
    public static function wrap($str, $max_len) {
        $arr = [];
        $len = mb_strlen($str);
        if ($len == 0) {
            return $arr;
        }
        if ($len <= $max_len) {
            $arr[] = $str;
            return $arr;
        }
        $w_len = 0;
        $w_str = "";
        $last_index = 0; // 上一次出現不是字母的索引位置
        for ($i = 0; $i < $len; $i++) {
            $sub_str = mb_substr($str, $i, 1); //將單個字符存到數組當中
            $w_str .= $sub_str;
            $w_len++;
            if (!self::isWord($sub_str)) {
                // 記錄最後一次出現不是字母的索引
                $last_index = $w_len;
            }
            // 需要換行
            if ($w_len >= $max_len) {
                if (self::isWord($sub_str)) {
                    $w_str_1 = mb_substr($w_str, 0, $last_index);
                    $w_str_2 = mb_substr($w_str, $last_index, $w_len);
                    $w_len = $w_len - $last_index;
                } else {
                    $w_str_1 = $w_str;
                    $w_str_2 = "";
                    $w_len = 0;
                }
                $arr[] = $w_str_1;
                $w_str = $w_str_2;
                $last_index = 0;
            }
        }
        $arr[] = $w_str;

        return $arr;

    }

    /**
     * 中文換行
     */
    public static function wrapCh($str, $max_len) {
        $arr = [];
        $len = mb_strlen($str);
        if ($len == 0) {
            return $arr;
        }
        if ($len <= $max_len) {
            $arr[] = $str;
            return $arr;
        }
        $page = ceil($len / $max_len);
        for ($i = 0; $i < $page; $i++) {
            $temp_str = mb_substr($str, $i * $max_len, $max_len);
            $arr[] = $temp_str;
        }
        return $arr;
    }

    /**
     * 判斷是否單詞
     */
    public static function isWord($chr) {
        $wordArr = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
        return in_array($chr, $wordArr);
    }

}

執行效果

    // 字符串
    $str = "呵呵呵發鏈接The British Charles Babbage was the inventor of an analytical machine with附加費兩款手機 punch發vvvvv個版本發vv cards, and of a difference engine, both forerunners of the computer, and his name is given to the mwei or picoether, 10 to the -12 ether.";
    $arr = StringUtils::wrapCh($str , 20);
    echo "<br/>暴力換行<br/><br/>";
    foreach($arr as $k=>$v){
        echo $v . "<br/>";
    }
    $arr = StringUtils::wrap($str , 20);
    echo "<br/>正確的換行<br/><br/>";
    foreach($arr as $k=>$v){
        echo $v . "<br/>";
    }

打印結果

暴力換行

呵呵呵發鏈接The British Ch
arles Babbage was th
e inventor of an ana
lytical machine with
附加費兩款手機 punch發vvvvv個
版本發vv cards, and of 
a difference engine,
both forerunners of
the computer, and h
is name is given to 
the mwei or picoethe
r, 10 to the -12 eth
er.

正確的換行

呵呵呵發鏈接The British 
Charles Babbage was 
the inventor of an 
analytical machine 
with附加費兩款手機 punch發
vvvvv個版本發vv cards, 
and of a difference 
engine, both 
forerunners of the 
computer, and his 
name is given to 
the mwei or 
picoether, 10 to 
the -12 ether.

圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章