1

前言

最近开发遇到后端生成图片时英文的换行

贴代码

/*
 * 字符串相关处理
 */
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.

图片描述


lmxdawn
1.4k 声望84 粉丝

你今生有没有坚定不移地相信过一件事或一个人?是那种至死不渝的相信?