Home >Backend Development >PHP Problem >How to read the last few characters of a string in php

How to read the last few characters of a string in php

青灯夜游
青灯夜游Original
2022-04-22 20:31:063043browse

In PHP, you can use the substr() function to read the last few characters of a string. You only need to set the second parameter of the function to a negative value and omit the third parameter; syntax "substr(string,-n)" means reading all characters from the nth character forward from the end of the string to the end of the string.

How to read the last few characters of a string in php

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

In php, you can use substr () function to read the last few characters of the string.

substr() function returns a part of a string.

You only need to omit the third parameter of the function, and set the second parameter to a negative value (-n), n specifies the last few characters you want to read

Implementation example :

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "abcdefg";
echo "原字符串:".$str."<br>";
echo "读取后1个字符:".substr($str,-1)."<br>";
echo "读取后2个字符:".substr($str,-2)."<br>";
echo "读取后3个字符:".substr($str,-3)."<br>";
?>

How to read the last few characters of a string in php

Description:

substr() function can intercept a certain length of characters from the specified position of the string. This paragraph The intercepted characters can be called "substrings" or "substrings", and their syntax format is as follows:

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

Parameter description is as follows:

  • $string: required The intercepted string contains at least one character;
  • $start: intercepts the starting position of the string;
    • If $start is a non-negative number, then the string will be from $string Interception starts at the $start character, 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.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to read the last few characters of a 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