How to solve the problem of intercepting strings with different lengths

Source: Internet
Author: User
Truncates a string. the length is different from the string. the length is different. & lt ;? PHP & nbsp; $ prna = $ row ['description']; & nbsp; if (strlen ($ prna) & gt; 20) {$ prna = mb_substr ($ prna, 0,40, 'utf-8 '). "... truncates a string with different lengths.

Truncates a string with different lengths.



   if(strlen($prna)>20)
{
$prna=mb_substr($prna,0,40,'utf-8')."...."; 
echo $prna;
}
else
{
echo $prna;
}   
   
   
   ?>



I often cannot upload files. I don't know if the computer is faulty.

Result:
MicroSD producer Produo DUO producer ....
PSP3000 sort (3 parts )....
IPhone3GS SIM card (black )....
Nokia CA-101D short MicroUSB data transmission ....
PSP2000 uses the background middleware (black )....
PSP2000 power source connection ....
3.5 Serial plug hoist (black )....
IPhone transparent plug (3) ...... share to: 20) {$ prna = mb_subst... 'data-pics = ''>
------ Solution --------------------
This is best solved with css (by container width)

Because the string length is not equal to the pixel width
Even if your local display is aligned, the font used by the client is uncontrollable, which means that the pixel width is still variable.
Change the default font in the browser and you will understand.
------ Solution --------------------
Your computing is faulty.
Generally, the text on the page is displayed in the default format. Therefore, the display width of a Chinese character is considered to be the width of two letters.
If the font adopts a non-equal width font, the situation will be much more complicated and will not be discussed here

1 byte for letters and symbols
For gbk encoding, a Chinese character is 2 bytes, which exactly meets the above situation
For UTF-8 encoding, one Chinese character is 3 bytes, that is, each Chinese character requires an additional 1 byte, and the number of Chinese characters x 2/3 is required during the calculation of bytes.

Mb_substr is intercepted by characters. both Chinese and Western are considered as one. Gourd occupancy factor

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.