search
HomeBackend DevelopmentPHP ProblemHow to read the last few characters of a string in php

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool