Is the title too long to be truncated? How to separate the full and half corners to avoid garbled characters _ PHP-php Tutorial

Source: Internet
Author: User
Author: lemann (lemann) functionChgTitle ($ title) {$ length46; if (strlen ($ title) $ length) {$ temp0; for ($ i0; $ I $ length; $ I ++) if (ord ($ title [$ I]) 128) $ temp ++; if ($ temp % 20) $ titlesubstr ($ title author: lemann (lemann)

Function ChgTitle ($ title)

{



$ Length = 46;

If (strlen ($ title)> $ length ){

$ Temp = 0;

For ($ I = 0; $ I <$ length; $ I ++)

If (ord ($ title [$ I])> 128)

$ Temp ++;

If ($ temp % 2 = 0)

$ Title = substr ($ title, 0, $ length )."...";

Else

$ Title = substr ($ title, 0, $ length + 1 )."...";

}

Return $ title;

}



The principle is to truncation a character to see if its ascII code is greater than 128. If yes, it indicates that the truncation is a full-angle Chinese character, then a truncation is taken back. Use $ length to control the length



Note: cyclically Judge> 128 in a string

The number of characters. if The halfwidth character is an even number, it indicates that the position is exactly the whole Chinese character. if it is an odd number, it is half a Chinese character and the next word is required.

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.