Collection of methods for intercepting php, js, and css strings, jscss

Source: Internet
Author: User
Tags truncated

Collection of methods for intercepting php, js, and css strings, jscss

There may be no content. Please make less tiles.
The first is the PHP version.

Copy codeThe Code is as follows:
<? Php echo mb_strimwidth ("here is the content", "...", "UTF-8");?>

In fact, only one mb_strimwidth function can be used. The description of this function is as follows:
Mb_strimwidth-get the 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 (that is, the original string, the output string)
$ Start is truncated starting from the nth character. The default value is 0.
$ Width the width to be trimmed
$ After the trimmarker is intercepted, the content added to the end of the string (commonly referred to as... indicates that the content is omitted). By default, I am empty.
$ Encoding: this parameter is very important. If the string is Chinese, you must add it. Otherwise .... You can see "��". I haven't carefully read this function before. In the wordpress topic, because I want to display a short section of the article, there will be garbled characters at the end, I don't know why for a long time. In addition, this parameter should be consistent with the webpage encoding format. In my personal test, the webpage encoding is UTF-8, and the Chinese character is shit when the parameter is written as gbk .. (Could you please explain)
This is the case with the php version. Sometimes we think it is a problem with the php language, but we did not study it carefully.

Js version:

There is almost * no difference between the substring () and substr () methods,
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 stringObject, which is one more character. It is not included by default and ends at the end of the string.
The first parameter of substr () is required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-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. It must be a numerical value. If this parameter is omitted, the string from the start position of the stringObject to the end is returned.
Example:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var str = "Hello world! "
Document. write (str. substring (3 ))
</Script>

Output in this example: lo world!
Start from the third digit of the original string and end

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var str = "Hello world! "
Document. write (str. substring (3, 7 ))
</Script>

Output in this example: lo w
Starting from the fourth digit of the original string to the seventh digit

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var str = "Hello world! "
Document. write (str. substr (3 ))
</Script>

Output: lo world!
Third, start to end

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var str = "Hello world! "
Document. write (str. substr (3, 7 ))
</Script>

Output: lo worl
Start from the fourth digit and take 7 digits.

JS.
Http://www.w3school.com.cn/js/jsref_substring.asp
Http://www.w3school.com.cn/jsref/jsref_substr.asp

The third is CSS.
CSS truncation mainly uses the text-overflow attribute.
Text-overflow: [clip | ellipsis | <string>]

The default value of text-overflow is clip. When the content exceeds the container, the excess text is cut out. If the value is ellipsis, the excess text is replaced by a ellipsis; you can also use a specific string to replace the excess text (currently only supported by firefox ).

Example of ellipsis:
Copy codeThe Code is as follows:
. Ellipsis {
Overflow: hidden;
White-space: nowrap;
Text-overflow: ellipsis;
}

[Copy] References:
Http://quirksmode.org/css/user-interface/textoverflow.html
Https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow

In fact, you can see the legend on the mozilla developer website for css instructions. I will not talk about it here.


How to intercept a string using a js string

Str = "a, B, c ";
S = str. split (",");

The split method can split strings into arrays to meet your requirements.

Php intercepts a string before a specified character

You can use the substr function and strrpos function to obtain the new a value using the following expression:

$ Newa = substr ($ a, 0, strrpos ($ a, '&'); // new $ a Value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.