Home > Article > Web Front-end > Collection of methods to intercept strings in php, js, css_javascript skills
It may not contain much content, please don’t buy it.
The first is the PHP version.
In fact, you only need to use the mb_strimwidth function. The description of this function is as follows:
mb_strimwidth — Get a string truncated by the 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. original string, output string)
$start starts intercepting characters, the default is 0
$width The width to be trimmed
$trimmarker After interception, the content added at the end of the string (commonly... means omitted), the default is empty
The $encoding parameter is very important. If the string is Chinese, it must be added. otherwise. . . . You can see the "�" thing. I haven't looked at this function carefully before. In the WordPress theme, because it needs to display a small section of the article, there are garbled characters at the end. I don't know why for a long time. In addition, this parameter should be consistent with the encoding format of the web page. During personal testing, the web page was encoded as utf-8. When the parameter was written as gbk, the Chinese characters would be shit. . (please ask for explanation)
That's it for the PHP version. Sometimes I think it's a problem with the PHP language, but in fact it's just that we didn't study it carefully.
js version:
substring() and substr() methods, there is *almost* no difference between the two methods,
The first parameter of the substring() method is required and is the position of the first character of the substring to be extracted in the string. The second parameter is optional and is the last character of the substring to be extracted in the stringObject. The position has one more digit, defaults to none, and reaches the end of the string.
The first parameter of substr() is required. The starting index of the substring to be extracted. Must be a numeric value. If negative, this 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, the string from the beginning to the end of stringObject is returned.
Example:
This example outputs: lo world!
Starting from the third digit of the original string to the end
This example output: lo w
Starting from the fourth digit of the original string to the seventh digit
Output: lo world!
Starting from the third position to the end
Output: lo worl
Starting from the fourth digit, intercept 7 digits.
You can see these two JS methods
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 attribute.
text-overflow: [ clip | ellipsis |
The default value of text-overflow is clip, that is, when the content exceeds the container, the excess text will be cut off. When the value is ellipsis, the excess text will be replaced with ellipses; you can also use a specific string to replace the excess text. Text (currently only supported by Firefox).
Example of ellipsis:
[copy]Reference:
http://quirksmode.org/css/user-interface/textoverflow.html
https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow
In fact, the description of css can be understood by looking at the legend on the mozilla developer website. Not much to say here.