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.