How to get the Last n Characters of a PHP String


What is PHP?

PHP (Hypertext Preprocessor) is a widely used server-side scripting language for web development. It allows developers to embed code within HTML files, enabling the creation of dynamic web pages and interactions with databases. PHP is known for its simplicity, versatility, and extensive integration capabilities with popular databases. It offers a broad range of extensions and has a large community of developers, ensuring ample resources and support. PHP is commonly employed in content management systems and e-commerce platforms, providing essential tools for handling data, processing user input, and generating dynamic website content.

How to get the last n Characters of a PHP string?

To get the last n characters of a PHP string, you can use the substr() function. The substr() function allows you to extract a portion of a string based on the starting position and length.

Using Substr() function

The substr() function in PHP is used to extract a portion of a string. It allows you to retrieve a specific substring based on the starting position and length provided. The function takes two required parameters: the input string and the starting position. Additionally, an optional third parameter can be used to specify the length of the substring to be extracted.

Syntax

The syntax of the substr() function is as follows:

substr(string $string, int $start, ?int $length = null): string|false
  • $string: The input string from which the substring will be extracted.

  • $start: The starting position of the substring. A positive value counts from the beginning of the string, while a negative value counts from the end.

  • $length (optional): The length of the substring to be extracted. If omitted, the function extracts all characters from the starting position to the end of the string.

The function returns the extracted substring as a string. If an error occurs, it returns false.

Example

Here's an example of how you can use substr() to get the last n characters of a PHP string:

<?php
$string = "Hello, World!";
$n = 6; // Number of characters to extract from the end
$lastNCharacters = substr($string, -$n);
echo $lastNCharacters; // Output: "World!"
?>

Output

World

Step by Step Explanation of Code

Let's go through the code step by step and explain what each line does:

This block of code begins with the opening PHP tag <?php. It is used to indicate the start of PHP code. The variable $string is assigned the value "Hello, World!", which is the input string we want to work with. The variable $n is set to 6, representing the number of characters we want to extract from the end of the string.

$lastNCharacters = substr($string, -$n);

This line uses the substr() function to extract the last 5 characters from the string.

The negative value -6 is used as the starting position to count from the end of the string. The result of substr($string, -$n) is assigned to the variable $lastNCharacters, which will store the extracted substring.

echo $ lastNCharacters; // Output: "World!"

The echo statement is used to display the value of $lastNCharacters to the screen. In this case, it will output "World!", which represents the last 6 characters of the original string "Hello, World!". You can modify the value of $string and $n to fit your specific needs.

Using strlen() function

https://www.geeksforgeeks.org/how-to-get-the-last-n-characters-of-a-php-string/

Conclusion

In conclusion, the provided PHP code showcases the usage of the substr() function to extract the last n characters from a string. By passing the input string and a negative value representing the number of characters to extract, the substr() function returns a substring starting from the specified position until the end of the string. The resulting substring is then displayed using echo. The code demonstrates a practical example of how to retrieve the last n characters of a PHP string using the substr() function.

Updated on: 31-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements