ThinkSNS + is how to calculate the length of the character display

+ What is ThinkSNS
  ThinkSNS (referred to as TS), a fully integrated platform for the social system, the provision of social software development and technical solutions for domestic and foreign medium and small enterprises and entrepreneurs, the latest version ThinkSNS +, ThinkSNS V4, ThinkSNS [profiles] .

Today we talk about something a lot of people headaches may: display length.

Demand is such that the characters on the show, accounting for only two English words show the length of a Chinese or other languages. as follows:

ab
哈
?

The above two rows of letters, a character, a Emoji. You will find, on account of the width of the display is the same. Some attractive design for such a process is also required.

For example, our demand is the demand for user names up to 12 non-single-byte characters or 24 single-byte characters can also be mixed rows of demand, we have to write back-end processing such verified.

Demand rule is / ^ [A-zA-the Z- \ x7f- \ xFF] [A-zA-Z0-9 \ x7f- \ xFF] * $ /

In ThinkSNS + in order to be able to verify that the common part, so choose to use custom validation rules. Let's start with the realization of ideas calculated under the bar!

First of all, even mb_strlen would not be able to obtain accurate multi-byte characters and single-byte characters mixed with the length, there is a saying online, accounting for three-byte characters, half-width symbols English one byte array, so:

`(mb_strlen($str) + strleng($str)) / 2`

This method can be obtained with a single byte of multibyte accounting calculation of from 0.5 to 1. But the Chinese, for example, only 20,000 Chinese characters is the case, there are more than sixty thousand four characters are followed, emoji is four bytes. Simply can not be calculated accurately.

Fortunately, I found a strange thing unwittingly str_word_count This function calculates the outside in addition to non-English words are symbols such as Chinese characters is in accordance with the number counted, emoji is the same reason.

After this discovery, it will be easier. Username us in [a-aA-Z0-9_] weed out, not counting individual is to verify the length we want it?

So, first we use:

preg_match_all('/[a-zA-Z0-9_]/', $value, $single);
$single = count($single[0]) / 2;

Calculated as a separate length of single-byte character display, and then:

$double = str_word_count(preg_replace('([a-zA-Z0-9_])', '', $value));

Calculated as a multi-byte length, and finally:

$length = $single + $double;

To obtain a display length, achieved, and finally encapsulated into validation rules:

Validator::extend('display_length', function ($attribute, $value, array $parameters) {
                        if (empty($parameters)) {
                throw new \InvalidArgumentException('Parameters must be passed');
            }

            $min = 0;
            if (count($parameters) === 1) {
                list($max) = $parameters;
            } elseif (count($parameters) >= 2) {
                list($min, $max) = $parameters;
            }

            if (! isset($max) || $max < $min) {
                throw new \InvalidArgumentException('The parameters passed are incorrect');
            }

            // 计算单字节.
            preg_match_all('/[a-zA-Z0-9_]/', $value, $single);
            $single = count($single[0]) / 2;

            // 多子节长度.
            $double = str_word_count(preg_replace('([a-zA-Z0-9_])', '', $value));

                        // 得出最终计算字符的长度
            $length = $single + $double;

            return $length >= $min && $length <= $max;
});

Code is the prototype code, not optimized, and then we just follow the following manner:

$rules = [
    'inputKey' => 'display_length:5', // 表示 0 - 5 显示长度
    ‘inputkey2’ => 'display_length:4,12' // 表示显示长度为 4 - 12
];

Now it is a good solution to this need.

We are very happy, will share to you based on Laravel of ThinkSNS + product development technology solutions, and hope like a friend can give the country a little bit of support open-source products.

Past research Diaries Review:

"ThinkSNS + Laravel master branch based on a diary [research and development]."

"ThinkSNS + R & D in front of the choice (webpack / Vue) stepped pit development diary diary [two]."

"ThinkSNS + Component-based development Laravel Route diary of [three]."

"How do Laravel configuration can be configured website background research diary [four]."

Open source code repository:

GitHub:https://github.com/zhiyicx/thinksns-plus (Click star, concerned about the development of dynamic daily.)

Open source is not easy, in order to gain revenue, our team made a lot of effort. Laravel based on the works displayed in front of everyone, after the column will be continuing to share technical details ThinkSNS + development process.

Guess you like

Origin blog.51cto.com/14231620/2412310