In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
What is ThinkSNS+?
ThinkSNS (TS for short), a platform-wide comprehensive social system, provides social software development and technical solutions for large, small and medium-sized enterprises and entrepreneurs at home and abroad. The latest versions are ThinkSNS+, ThinkSNS V4, ThinkSNS [Jane].
Today we're going to talk about something that a lot of people might have a headache: display length.
The requirement is that in the display of characters, two English words account for the display length of one Chinese or other language. As follows:
Ab, huh?
There are two English letters, one Chinese character and one Emoji. You will find that the width of the display is the same. Some designs require this kind of treatment in order to look good.
For example, our user name requirement is that a maximum of 12 non-single-byte characters or 24 single-byte characters can also be mixed with row requirements, and we have to deal with such verification at the back end.
The requirement rule is / ^ [a-zA-Z\ x7f -\ xff] [a-zA-Z0-9\ x7f -\ xff] * $/
In ThinkSNS+, in order to make this part of the validation public, you choose to use custom validation rules. Let's first talk about the implementation of computing ideas!
First of all, even mb_strlen cannot accurately obtain the length of a mixture of multi-byte characters and single-byte characters. There is a saying on the Internet that Chinese characters account for three bytes and English array half-width symbols account for one byte, so:
`(mb_strlen ($str) + strleng ($str)) / 2`
Using this method, we can get the calculation that single byte accounts for 0.5 multi-byte. But take Chinese as an example, only 20, 000 Chinese characters are the case, there are more than 60, 000 Chinese characters are four, and secondly, emoji is also four bytes. It is impossible to calculate accurately at all.
Fortunately, I stumbled upon a strange thing: str_word_count this function calculates non-English words in addition to symbols such as Chinese, which is calculated according to the number of Chinese characters, and emoji is the same.
When we found this, it was easy for us to do. Let's remove [a-aA-Z0-9] from the user name and calculate the verification length separately.
So, first of all, we use:
Preg_match_all ('/ [a-zA-Z0-9 _] /', $value, $single); $single = count ($single [0]) / 2
Method to calculate the display length of single-byte characters separately, and then use:
$double = str_word_count (preg_replace ('([a-zA-Z0-9 _])','', $value))
Method to calculate the length of multiple bytes, and finally:
$length = $single + $double
The display length is obtained, implemented, and finally encapsulated into verification 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 'display_length:5', / / indicates 0-5 display length' inputkey2' = > 'display_length:4,12' / / indicates display length 4-12]
This need has been well addressed now.
We are happy to share with you the technical solutions in the development of Laravel-based ThinkSNS+ products, and hope that our favorite friends can give a little support to the domestic open source products.
Review of previous R & D diaries:
"ThinkSNS+ is based on the Laravel master branch [R & D Diary I]"
"webpack/Vue at the front end of ThinkSNS+ R & D [R & D Diary II]
" ThinkSNS+ Component based on Laravel Route [R & D Diary 3]"
"how to configure Laravel can be configured at the backend of the website [R & D Diary 4]"
Open the source code repository:
GitHub: https://github.com/zhiyicx/thinksns-plus (click star to follow developments on a daily basis. )
Open source is not easy, in order to strive for open source, our team has made a lot of efforts. Show the Laravel-based work in front of you, and the column will continue to share the technical details of the ThinkSNS + development process.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.