How to intercept Chinese strings in cakephp

Source: Internet
Author: User
Tags truncated
How do I intercept Chinese strings in cakephp?
Originally thought to write the helper to intercept the Chinese string, did not expect CakePHP's text helper class has provided the Truncate method. The following is a prototype of the Truncate method.

Reference

Truncate
Truncate (string $text, int $length =100, array $options)
Cuts a string to the $length and adds a suffix with ' ending ' if the text is longer than $length. If ' exact ' is passed as false, the truncation would occur after the next word ending. If ' HTML ' is passed as true, HTML tags would be respected and would not be cut off.



The Text->truncate method has 3 parameters:

    • $text: a string to intercept
    • $length: Length to intercept, 100 characters by default
    • $options: Array parameters. Ending indicates that the trailing string is prefixed with a ending string, and exact is not truncated if it is false word;html if True then HTML tag is not truncated

The following code demonstrates how to use the Tuncate method:

echo $this->text->truncate (    ' The killer crept forward and tripped on the rug. ', ', ',    Array (        ' Ending ' + ' ... ',        ' exact ' = False    );


The above code will output The killer crept ...
Note: If you are intercepting Chinese strings, exact should be set to true, otherwise Chinese characters will not be truncated.
  • Related Article

    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.