How to intercept Chinese string in cakephp?

黄舟
Release: 2023-03-03 16:20:02
Original
1020 people have browsed it

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
    )
);
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template