Today PHP, I will show short tip how to remove last characters of string in PHP by using simple function. This guide will help you to delete last amount of characters of a text. For example we have a sentence and we want to remove last 5 characters of the sentence then we will use function in example below.
Basically, we may use built-in function substr()
to remove character where you begin to remove character and the index where we want to end remove the text. Now we will use this function again to reversly remove string from backward. Let's have a look in the example below.
<?php
$string = "Hello this is cambotutorial.com";
$last2Char = substr($string, 0, -2);
echo $last2Char."<br>";
$last3Char = substr($string, 0, -3);
echo $last3Char."<br>";
$last4Char = substr($string, 0, -4);
echo $last4Char."<br>";
$last5Char = substr($string, 0, -5);
echo $last5Char."<br>";
$last7Char = substr($string, 0, -7);
echo $last7Char."<br>";
Output:
Hello this is cambotutorial.c
Hello this is cambotutorial.
Hello this is cambotutorial
Hello this is cambotutoria
Hello this is cambotutor
This substr()
accept 3 parameters. First is the string that we want to remove characters from, secondly is the index where to start. The last is reverse index character where we want to remove. Thanks for reading, please have a nice day!!.
You might also like...
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him