I originally thought I would write a helper to intercept Chinese strings, but I didn’t expect that the truncate method is already provided in cakephp’s Text helper class. Below is the prototype of the truncate method.
Quote
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 will occur after the next word ending. If 'html' is passed as true, html tags will be respected and will not be cut off.
Text->truncate method There are 3 parameters:
$text: the string that needs to be intercepted
$length: the length that needs to be intercepted, the default is to intercept 100 characters
$options: array parameters. ending means adding the ending string to the end of the intercepted string; if exact is false, the word will not be truncated; if html is true, the html tag will not be truncated
The following code demonstrates how to use the tuncate method:
Php generation
echo $this->Text->truncate( 'The killer crept forward and tripped on the rug.', 22, array( 'ending' => '...', 'exact' => false ) ); echo $this->Text->truncate( 'The killer crept forward and tripped on the rug.', 22, array( 'ending' => '...', 'exact' => false ) );
The above code will output The killer crept...
Note: If you are intercepting a Chinese string, it is best to set exact to true, otherwise the Chinese characters will not be truncated
The above is how to intercept Chinese in cakephp The content of the string, please pay attention to the PHP Chinese website (www.php.cn) for more related content!