"PHP: Get Last 4 Characters with substr()"

Sandra Jun 29, 2026

In the realm of web development, PHP is a powerful scripting language that enables dynamic and interactive websites. One of its many string manipulation functions is substr(), which allows you to extract a substring from a given string. Today, we're going to delve into a specific use case: extracting the last 4 characters from a string using PHP's substr() function.

PHPSpreadsheet Quickstart Tutorial
PHPSpreadsheet Quickstart Tutorial

Understanding PHP's substr() Function

Apocalypse Survivor Art, Apocalypse Survivor Concept Art, Zombie Apocalypse Survivor Art, Post Apocalyptic Survivor Concept Art, Zombie Survivor Art, Zombie Apocalypse Survivor Drawing, Apocalyptic Survivor Concept Art, Zombie Survivor Concept Art, Post Apocalyptic Bounty Hunter
Apocalypse Survivor Art, Apocalypse Survivor Concept Art, Zombie Apocalypse Survivor Art, Post Apocalyptic Survivor Concept Art, Zombie Survivor Art, Zombie Apocalypse Survivor Drawing, Apocalyptic Survivor Concept Art, Zombie Survivor Concept Art, Post Apocalyptic Bounty Hunter

Before we dive into extracting the last 4 characters, let's ensure we understand the basics of the substr() function. It takes three arguments: the string from which you want to extract a substring, the starting position, and the length of the substring. The starting position is 0-indexed, meaning the first character is at position 0.

Here's the basic syntax:

Division 2 Agents, The Division 2 Black Tusk Faction, The Division 2 Rogue Agent Wallpaper, The Division 2 Shd Watch, The Division 2 Agent, Rogue Agents The Division 2, Division Agent Watch, Rogue Agents Division 2, The Division 2 Watch
Division 2 Agents, The Division 2 Black Tusk Faction, The Division 2 Rogue Agent Wallpaper, The Division 2 Shd Watch, The Division 2 Agent, Rogue Agents The Division 2, Division Agent Watch, Rogue Agents Division 2, The Division 2 Watch

substr(string, start, length)

Extracting the Last 4 Characters: A Step-by-Step Guide

To extract the last 4 characters from a string, we need to determine the starting position and length. Since we want the last 4 characters, the length is straightforward: it's 4. Determining the starting position, however, requires a bit of calculation.

a man with white hair and tattoos standing in front of a car on the street
a man with white hair and tattoos standing in front of a car on the street

Calculating the Starting Position

The starting position is the length of the string minus the number of characters we want to extract, minus 1 (because PHP uses 0-based indexing). Here's how you can calculate it:

Starting position = strlen(string) - 4 - 1

Ghost In A Shell Characters, Ghost In The Shell Police, Sci Fi Military Concept Art, Motoko Kusanagi Robot, Ghost In The Shell Character Design, Major Kusanagi, Ghost In The Shell Soldier, Gits Major, Ghost In The Shell Armor
Ghost In A Shell Characters, Ghost In The Shell Police, Sci Fi Military Concept Art, Motoko Kusanagi Robot, Ghost In The Shell Character Design, Major Kusanagi, Ghost In The Shell Soldier, Gits Major, Ghost In The Shell Armor

Where strlen() is another PHP function that returns the length of a string.

Putting It All Together

Now that we have the starting position and length, we can use substr() to extract the last 4 characters. Here's an example:

Cod Infinite Warfare Characters, Call Of Duty Infinite Warfare Characters, Advanced Warfare Characters, Ghost Recon Breakpoint Female Character, Titanfall 2 Female Pilot, Call Of Duty Manta Ray, Female Division Agent, Resident Evil Jill Battlesuit, Call Of Duty Advanced Warfare Ilona
Cod Infinite Warfare Characters, Call Of Duty Infinite Warfare Characters, Advanced Warfare Characters, Ghost Recon Breakpoint Female Character, Titanfall 2 Female Pilot, Call Of Duty Manta Ray, Female Division Agent, Resident Evil Jill Battlesuit, Call Of Duty Advanced Warfare Ilona

```php ```

Real-World Applications

Extracting the last 4 characters can be useful in various scenarios. Here are a few examples:

Sam Fisher Outfit, R6 Sam Fisher, Sam Fisher Blacklist, Sam Fisher Cosplay, Sam Fisher Concept Art, Rainbow Six Siege Sam Fisher, Sam Fisher Ghost Recon, Sam Fisher, Sam Fisher Double Agent
Sam Fisher Outfit, R6 Sam Fisher, Sam Fisher Blacklist, Sam Fisher Cosplay, Sam Fisher Concept Art, Rainbow Six Siege Sam Fisher, Sam Fisher Ghost Recon, Sam Fisher, Sam Fisher Double Agent
a drawing of a woman with tattoos on her chest and arms, standing in front of a
a drawing of a woman with tattoos on her chest and arms, standing in front of a
Project Zomboid Faces, Post-apocalyptic Alias Chart, Fallout Sprite, Project Zomboid Character, Hellonearth Characters, Post-apocalyptic Wasteland Warfare Hmmwv, Post Apocalyptic Character Creator, Post Apocalyptic Survival Swap Gallery, Followers Of The Apocalypse Fallout
Project Zomboid Faces, Post-apocalyptic Alias Chart, Fallout Sprite, Project Zomboid Character, Hellonearth Characters, Post-apocalyptic Wasteland Warfare Hmmwv, Post Apocalyptic Character Creator, Post Apocalyptic Survival Swap Gallery, Followers Of The Apocalypse Fallout
Types of Operators In PHP (With Examples)
Types of Operators In PHP (With Examples)
three women with different facial expressions and hair in the same image, one is wearing a beige jacket
three women with different facial expressions and hair in the same image, one is wearing a beige jacket
Artificer Subclass Ideas, Dnd Artificer Subclass Ideas, 5e Artificer Subclass Ideas, Dnd 5e Artificer Subclass, 5e Artificer Subclass Guide, Dnd Artificer Subclass, Artificer Subclass Biomancy, Artificer Subclass, Dnd 5e Homebrew Artificer Subclasses
Artificer Subclass Ideas, Dnd Artificer Subclass Ideas, 5e Artificer Subclass Ideas, Dnd 5e Artificer Subclass, 5e Artificer Subclass Guide, Dnd Artificer Subclass, Artificer Subclass Biomancy, Artificer Subclass, Dnd 5e Homebrew Artificer Subclasses
Modern Soldier Character Art, Modern Mercenary Concept Art, Tactical Character Design Male, Starfinder Soldier, Scifi Character Design Male Soldier, Sci Fi Character Art Male Soldier, Sci Fi American Soldier, Cyberpunk Mercenary Male, Sci Fi Human Soldier
Modern Soldier Character Art, Modern Mercenary Concept Art, Tactical Character Design Male, Starfinder Soldier, Scifi Character Design Male Soldier, Sci Fi Character Art Male Soldier, Sci Fi American Soldier, Cyberpunk Mercenary Male, Sci Fi Human Soldier
Rebellion Star Wars Characters, Star Wars Rpg Characters, Star Wars Smuggler Concept Art, Star Wars Rpg Character Art, Star Wars Gunslinger Art, Star Wars Smuggler Art, Star Wars Scientist Character Art, Star Wars Rpg Art, Star Wars Gunslinger
Rebellion Star Wars Characters, Star Wars Rpg Characters, Star Wars Smuggler Concept Art, Star Wars Rpg Character Art, Star Wars Gunslinger Art, Star Wars Smuggler Art, Star Wars Scientist Character Art, Star Wars Rpg Art, Star Wars Gunslinger
DUP soldier Infamous Second Son Concept Art, Infamous Second Son Ign Guide, Infamous 2 Game Art, Infamous Second Son Vest, Infamous Second Son Augustine, Infamous Second Son Concrete Power, Eugene Sims Infamous Second Son, Infamous Second Son Graphics, Eugene Sims
DUP soldier Infamous Second Son Concept Art, Infamous Second Son Ign Guide, Infamous 2 Game Art, Infamous Second Son Vest, Infamous Second Son Augustine, Infamous Second Son Concrete Power, Eugene Sims Infamous Second Son, Infamous Second Son Graphics, Eugene Sims
Advanced Warfare Characters, Cod Ghosts Concept Art, Cod Advanced Warfare Concept Art, Call Of Duty Ghosts Concept Art, Resident Evil Uss Soldier, Medal Of Honor 2010 Characters, Advanced Warfare Atlas Soldier, Call Of Duty Advanced Warfare Concept Art, Modern Warfare 3 Outpost
Advanced Warfare Characters, Cod Ghosts Concept Art, Cod Advanced Warfare Concept Art, Call Of Duty Ghosts Concept Art, Resident Evil Uss Soldier, Medal Of Honor 2010 Characters, Advanced Warfare Atlas Soldier, Call Of Duty Advanced Warfare Concept Art, Modern Warfare 3 Outpost
Sci Fi Operative, Incursor Marksman, Sci Fi Vigilante, Dark Sector Armor, Sci Fi Mercenary, Operative Starfinder, Star Wars Police Speeder, Starfinder Soldier, Sci Fi Spear
Sci Fi Operative, Incursor Marksman, Sci Fi Vigilante, Dark Sector Armor, Sci Fi Mercenary, Operative Starfinder, Star Wars Police Speeder, Starfinder Soldier, Sci Fi Spear
Gith
Gith
Barbarian Paths Homebrew, Dnd Necromancer Homebrew, Necromancer 5e, Necromancer Homebrew 5e, Necromancer Class Dnd, Necromancer Stats, Dnd 5e Homebrew Barbarian Paths, Dnd Sorcerer Subclasses Homebrew, Barbarian Subclasses
Barbarian Paths Homebrew, Dnd Necromancer Homebrew, Necromancer 5e, Necromancer Homebrew 5e, Necromancer Class Dnd, Necromancer Stats, Dnd 5e Homebrew Barbarian Paths, Dnd Sorcerer Subclasses Homebrew, Barbarian Subclasses
Resident Evil Character List, Resident Evil 6 Agent, Video Game Character Names, Metal Gear Solid Timeline, Resident Evil Games List, Resident Evil Characters Age, Resident Evil Stars Bravo Team, Metal Gear With Dimensions, Resident Evil Revelations 2 Characters
Resident Evil Character List, Resident Evil 6 Agent, Video Game Character Names, Metal Gear Solid Timeline, Resident Evil Games List, Resident Evil Characters Age, Resident Evil Stars Bravo Team, Metal Gear With Dimensions, Resident Evil Revelations 2 Characters
Battlefield Pfp, Battlefield 4 Pfp, Metro Spartan Armor, Battlefield 3 Pfp, Cod Infinite Warfare Soldiers, Helldivers 2 Seaf Soldier, Bf3 Soldier, Heavy Gunner Clone Trooper, Bf3 Wallpaper
Battlefield Pfp, Battlefield 4 Pfp, Metro Spartan Armor, Battlefield 3 Pfp, Cod Infinite Warfare Soldiers, Helldivers 2 Seaf Soldier, Bf3 Soldier, Heavy Gunner Clone Trooper, Bf3 Wallpaper
an image of many different avatars in the same style and color scheme for each character
an image of many different avatars in the same style and color scheme for each character
an advertisement for the game's website, featuring a girl with tattoos on her arm and
an advertisement for the game's website, featuring a girl with tattoos on her arm and
a page with an image of a woman in armor
a page with an image of a woman in armor
Naruto Workout, Shadowrun Classes, Steel Lynx Shadowrun, Shadowrun Character Creation, Shadowrun 2e, Shadowrun Lonestar, Shadowrun Steel Lynx, Shadowrun Returns Upper Floor Walkthrough, Shadowrun Decker
Naruto Workout, Shadowrun Classes, Steel Lynx Shadowrun, Shadowrun Character Creation, Shadowrun 2e, Shadowrun Lonestar, Shadowrun Steel Lynx, Shadowrun Returns Upper Floor Walkthrough, Shadowrun Decker
an image of a block diagram with all the names and numbers in each column on it
an image of a block diagram with all the names and numbers in each column on it
  • File Extensions: You can extract the file extension (e.g., 'jpg', 'png', 'pdf') from a file path.
  • Date Formatting: You can extract the last 4 digits of a date string to get the year (e.g., '2022' from '2022-01-01').
  • String Truncation: You can use this method to truncate a string to a specific length, displaying ellipses (...) if the string is too long.

Practical Example: Truncating a String with Ellipses

Let's create a function that truncates a string to a specific length, appending ellipses (...) if the string is too long. We'll use our knowledge of extracting the last 4 characters to achieve this:

```php $length) { $truncated = substr($string, 0, $length - 3) . '...'; } else { $truncated = $string; } return $truncated; } $longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; $truncated = truncateString($longString, 20); echo $truncated; // Outputs: "Lorem ipsum dolor sit..." ?> ```

In this example, the function truncateString() checks if the input string is longer than the specified length. If it is, it extracts the first length - 3 characters (to make room for the ellipses), appends '...', and returns the result. If the string is not too long, it's returned as is.

This function demonstrates how understanding how to extract the last 4 characters can be applied to create more complex string manipulation functions.