A collection of methods for intercepting strings from PHP js and CSS

  • 2020-03-30 04:01:19
  • OfStack

There may be no content, less patting brick.
The first is the PHP version.


<?php echo mb_strimwidth(" Here is the content ", 0,3,"...","utf-8"); ?>

  In fact, only mb_strimwidth is a function, which is explained as follows:
Mb_strimwidth - gets the string truncated at 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 intercepted from the first character and is 0 by default
  $width the width required to trim
After intercepting $trimmarker, add the content at the end of the string. I am null by default
$encoding is an important parameter, so if the string is in Chinese, be sure to include it. Otherwise... You can see "�" This thing, I haven't looked at this function carefully before, in the wordpress theme because I want to display a piece of the article, and then at the end of the garbled code, I don't know why for a long time. In addition, this parameter should be consistent with the coding format of the web page, personal test when the web page code utf-8, when the parameter is written as GBK Chinese character shit. (ask Daniel to explain)
This is what the PHP version looks like, and sometimes we think it's just the PHP language, but we don't really study it.

Js version:

Substring () and substr() methods, the two methods * are almost * identical,
The first argument to the substring() method is required to be the position of the first character of the substring to be extracted in the string, and the second argument is optional to be the position of the last character of the substring to be extracted 1 bit more in the stringObject, none by default, to the end of the string.
The first parameter of substr() is required. The starting subscript of the substring to be extracted. It has to be a number. If it is negative, the argument declares the position from the end of the string. That is, -1 refers to the last character in the string, -2 to the penultimate character, and so on. The second parameter is optional. The number of characters in a substring. It has to be a number. If this parameter is omitted, the string from the beginning of the stringObject to the end is returned.
Example:


    <script type="text/javascript">
    var str="Hello world!"
    document.write(str.substring(3))
    </script>

This example output: lo world!
It starts at the third bit of the original string and ends at the end


<script type="text/javascript">
    var str="Hello world!"
    document.write(str.substring(3,7))
    </script>

The output of this example is lo w
It starts at the fourth bit of the original string and goes to the seventh bit


<script type="text/javascript">
    var str="Hello world!"
    document.write(str.substr(3))
    </script>

Output: lo world!
The third starts and ends


<script type="text/javascript">
    var str="Hello world!"
    document.write(str.substr(3,7))
    </script>

Output: lo worl
Starting at the fourth place, cut out 7 bits.

  So you can look at these two methods
(link: http://www.w3school.com.cn/js/jsref_substring.asp)
(link: http://www.w3school.com.cn/jsref/jsref_substr.asp)

The third one is CSS
CSS interception mainly USES the property text-overflow.
Text-overflow: [clip | ellipsis | <string>]

Examples of ellipsis:


.ellipsis{
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

Copy:
(link: http://quirksmode.org/css/user-interface/textoverflow.html)
(link: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow)

  Just look at the legend of the mozilla developer website to see how CSS is explained. I won't say much here.


Related articles: