The ultimate problem and perfect solution for calculating and intercepting string lengths in JavaScript and php

Source: Internet
Author: User
1. js uses unicode to calculate the length. For example, if a single byte is counted as 1 and a Chinese character is counted as 1, but normally we want to calculate two single byte values as 1. How can we calculate the length of the first solution, use regular expressions as follows/[\ u0x00-\ u0xff]/, naively thinking, so that we can match all unicode in this... syntaxHighlighter. al

1. js uses unicode to calculate the length. For example, if a single byte is counted as 1 and a Chinese character is counted as 1, but normally we want to calculate the length of two single byte values as 1. How can we calculate this length?

 


Solution 1: use regular expressions as follows/[\ u0x00-\ u0xff]/, naively thinking, so that all unicode can be matched between them. However, this regular expression does not match punctuation marks, such as spaces and commas. Therefore, you can only traverse charCodeAt using the following code:


 

function getStringWidth(s){         var length = 0;         for(var i=0;i
 
  255) {                 length++;             } else {                 length += 0.5;             }         }         return Math.ceil(length);     } function getStringWidth(s){  var length = 0;  for(var i=0;i
  
   255) {    length++;   } else {    length += 0.5;   }  }  return Math.ceil(length); } 
  
 


2. The length of php should be divided into encoding. I only say utf8, write php and return gbk, so I won't say anything.

How to calculate the length?

Regular Expression? [X00-xff] This stuff does not match the space comma, and cannot be calculated with ord as js, because ord only calculates one byte

Here is an excellent solution. I use mb_substr to cut one string from the end of each time, and then judge the difference between the length after the cut and the length before the cut. If the difference is 1, the length will be + 0.5, if the length is greater than 1, the length is + 1, and a ceil is finally obtained. The Code is as follows:


 

function get_string_width($s) {         $length = 0;         while(strlen($s)>0) {             $old_length = strlen($s);             $s = mb_substr($s, 0,-1,'utf8');             if(($old_length-strlen($s))==1) {                 $length += 0.5;             } else {                 $length += 1;             }         }         return ceil($length);     } function get_string_width($s) {        $length = 0;        while(strlen($s)>0) {            $old_length = strlen($s);            $s = mb_substr($s, 0,-1,'utf8');            if(($old_length-strlen($s))==1) {                $length += 0.5;            } else {                $length += 1;            }        }        return ceil($length);    }

The test is complete and meets the requirements.

 


3. Intercept

function cut_string($s,$l=140) {         $length = get_string_width($s);         if($length>$l) {             while(get_string_width($s.'...')<=$l) {                 $s = mb_substr($s, 0,-1,'utf8');             }             $s .= '...';          }         return $s;     } function cut_string($s,$l=140) {        $length = get_string_width($s);        if($length>$l) {            while(get_string_width($s.'...')<=$l) {                $s = mb_substr($s, 0,-1,'utf8');            }            $s .= '...';        }        return $s;    } 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.