It may not have much content, please give it less.
First of all, it's the PHP version.
The code copy is as follows:
<?php echo mb_strimwidth("Here is content", 0,3,"...","utf-8"); ?>
In fact, you can only use mb_strimwidth one function. The description of this function is as follows:
mb_strimwidth - Get string truncated by specified width
string mb_strimwidth ( string $str , int $start , int $width [, string $trimmarker = "" [, string $encoding = mb_internal_encoding() ]] )
Parameter description:
$str is the string to be truncated (i.e. the original string, the output string)
$start starts from the first character, the default is 0
$width The width required for trimming
After $trimmarker is intercepted, the content added at the end of the string (common for... means omitted), by default I i-i
The $encoding parameter is very important. If the string is in Chinese, it must be added. otherwise. . . . You can see the "�". I haven't read this function carefully before. In the wordpress theme, I want to display a small piece of content of the article, and then there is garbled code at the end. I haven't known why for a long time. In addition, this parameter should be consistent with the encoding format of the web page. When the personal test is performed, the web page code is utf-8, and when the parameter is written as gbk, the Chinese characters will shit. . (Please explain to Daniu)
This is how the php version is. Sometimes I think it is a problem with the php language, but in fact we just haven't studied it carefully.
js version:
Substring() and substr() methods, the two methods *almost* are no different.
The first parameter of the substring() method is required. It is the position of the first character of the substring to be extracted in the string. The second parameter is optional. It is the position of the last character of the substring to be extracted in the stringObject. None by default, and it is at the end of the string.
The first parameter substr() is required. The starting subscript of the substring to be extracted. Must be a numeric value. If it is a negative number, the parameter declares the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second to last character, and so on. The second parameter is optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, a string from the start position of stringObject to the end is returned.
example:
The code copy is as follows:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substring(3))
</script>
This example output: lo world!
Start from the third bit of the original string and end
The code copy is as follows:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substring(3,7))
</script>
This example output: lo w
Start from the fourth bit of the original string to the seventh bit
The code copy is as follows:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3))
</script>
Output: lo world!
The third position starts from the end
The code copy is as follows:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3,7))
</script>
Output: lo world
Starting from the fourth position, intercept 7 bits.
These two methods of JS can be seen
http://www.w3school.com.cn/js/jsref_substring.asp
http://www.w3school.com.cn/jsref/jsref_substr.asp
The third one is CSS
CSS interception mainly uses the text-overflow property.
text-overflow: [ clip | ellipsis | <string> ]
The default value of text-overflow is clip, that is, when the content exceeds the container, the excessive text will be cut off. When the value is ellipsis, the excess text will be replaced with an ellipsis; a specific string can also be used to replace the excess text (currently only supported by firefox).
Examples of ellipsis:
The code copy is as follows:
.ellipsis{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
[copy] Reference:
http://quirksmode.org/css/user-interface/textoverflow.html
https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow
In fact, if you look at the legend of the mozilla developer website, you will understand. I won't say much here.