Home  >  Article  >  Backend Development  >  Use PHP's substr() function to intercept part of the string and add an ellipsis at the end

Use PHP's substr() function to intercept part of the string and add an ellipsis at the end

WBOY
WBOYOriginal
2023-11-03 18:06:481211browse

Use PHPs substr() function to intercept part of the string and add an ellipsis at the end

Use PHP's substr() function to intercept part of the string and add an ellipsis at the end

In actual development, you often encounter situations where you need to intercept a string . PHP's substr() function is a very commonly used string interception function. This article will use a practical example to demonstrate how to use the substr() function to intercept part of a string and add an ellipsis at the end.

First, I will provide a sample code. Then I'll explain this code.

<?php
function truncateString($string, $length){
    if(mb_strlen($string, 'utf-8') > $length){
        $string = mb_substr($string, 0, $length, 'utf-8');
        $string .= '...';
    }
    return $string;
}

// 测试示例
$text = "Use PHPs substr() function to intercept part of the string and add an ellipsis at the end";
$truncatedText = truncateString($text, 10);
echo $truncatedText;
?>

The above code defines a function truncateString(). This function receives two parameters: the string to be intercepted and the length of the interception. mb_strlen() is used inside the function to obtain the length of the string to ensure that the processing of Chinese characters is accurate. If the length of the string exceeds the specified interception length, use the mb_substr() function to intercept part of the string. Finally, add an ellipsis to the end of the truncated string.

In the above example, we intercepted the string "Use PHP's substr() function to intercept part of the string and add an ellipsis at the end", and set the intercepted length is 10. The execution result is "Using PHP...", where the ellipsis indicates that part of the original string has been intercepted.

It should be noted that mb_strlen() and mb_substr() are used here instead of the ordinary strlen() and substr()Function. This is because Chinese characters are different from English characters when calculating length and intercepting strings. Therefore, if you do not use the mb_ function to process Chinese characters, the intercepted results may be incorrect.

To summarize, it is a common requirement to use PHP's substr() function to intercept part of a string and add an ellipsis at the end. We can easily implement this function when processing Chinese characters through mb_strlen() and mb_substr(). I hope the sample code in this article can be helpful to you.

The above is the detailed content of Use PHP's substr() function to intercept part of the string and add an ellipsis at the end. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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