Home>Article>Backend Development> How to intercept the last few characters of a specified string in php

How to intercept the last few characters of a specified string in php

青灯夜游
青灯夜游 Original
2021-09-28 18:07:25 14214browse

In PHP, you can use the substr() function to intercept the last few characters of the specified string. You only need to set the second parameter "$start" of the function to a negative number to start from the end of the string. Starting at the "$start" character from the front, all characters are intercepted; the syntax is "substr($str,$start)".

How to intercept the last few characters of a specified string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

When processing strings, sometimes we The string needs to be intercepted. In PHP, intercepting strings can be achieved through PHP's predefined function substr().

The substr() function can intercept a certain length of characters from a specified position in a string. The intercepted characters can be called "substring" or "substring". The syntax format is as follows:

substr($string, $start [, $length])

The parameter description is as follows:

  • $string: The string that needs to be intercepted, the string contains at least one character;
  • $start: The string to be intercepted The starting position;
    • If $start is a non-negative number, then the string will be intercepted from the $start character of $string, and $start starts counting from 0. For example, in the string "abcdef", the character at position 0 is "a", the character at position 2 is "c", etc.;
    • If $start is a negative number, the string will start from $ The end of string starts at the $start character forward, and $start starts counting from -1. For example, in the string "abcdef", the character at position -1 is "f", the character at position -3 is "d", etc.;
    • If the length of $string is less than $start, it will be returned FALSE.
  • $length: Optional parameter, indicating the length of the intercepted string.
    • If $length is a positive number, then the string will be truncated up to $length characters from the $start position;
    • If $length is a negative number, then $length characters from the end of $string Characters will be omitted (if $start is a negative number, it will be counted from the end of the string);
    • If the value of $length is 0, FALSE or NULL, an empty string will be returned;
    • If $length is not provided, the returned substring will start from the $start position until the end of the string.

Example: intercept the last few characters of the specified string

If you want to intercept the last few characters of the specified string, just need Set the second parameter "$start" of the substr() function to a negative number to intercept all characters starting from the "$start" character from the end of the string.

Output:

How to intercept the last few characters of a specified string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to intercept the last few characters of a specified string in php. 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