Remove First 2, 3, 4, 5, 10, etc, Character From String PHP

Remove First 2, 3, 4, 5, 10, etc, Character From String PHP

PHP remove the first character from a string. In this tutorial, you will learn to remove the first character from string in PHP with examples.

In PHP, there are several ways to remove the first character from a string. The most commonly used methods are the substr() function, ltrim and the substr_replace() function. These functions have their own syntax and parameters that can be used to remove the first character from a string.

This tutorial shows you a simple and easy method for delete or remove first 1, 2, 3, 4, 5, 10, etc., characters from string in PHP.

To Remove First Character From String PHP

Below are various methods to remove the first character from a string in PHP; is as follows:

  • Method 1: substr function
  • Method 2: ltrim() function
  • Method 3: Using substr_replace() function

Method 1: substr function

You can use the substr function of PHP for remove the first character from string in PHP.

Syntax:

The syntax of subster method is given below:

substr($string);

Example1 – Method First – substr function

 $string = "Hello World!";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . substr($string, 1) . "\n"; 

Output-1

 Given string: Hello World!
 Updated string: ello World

In this example, you have assigned the string “Hello World!” to the variable $string. Then you have used the substr() function to extract a part of the string starting from the second character, which is the letter “e”. The result is then assigned to the variable $newString, which is then printed to the screen using the echo statement.

Example2 – PHP substr function

Suppose you have one string like this “Hi, You are a good programmer.” . In this string you want to remove first three characters in PHP. So you can use the substr() function like this:

 $string = "Hi, You are a good programmer.";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . substr($string, 3) . "\n"; 

Using the above example2 of method substr, you can remove first n number of characters from string in PHP.

Output-2

 Given string: Hi, You are a good programmer.
 Updated string: You are a good programmer.

Method 2: ltrim() function

You can use the PHP ltrim() function to remove the first character from the given string in PHP.

Syntax:

The basic syntax of ltrim() function is:

 ltrim($string,'h');

Here “h” is the character that you want to remove in your string.

Example – ltrim() function

 $string = "Hello World!";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . ltrim($string, "!") . "\n";

Output

 Given string: Hello World!
 Updated string: ello World  

In the above code, you first declare a string variable $str with the value “Hello World”. Then you use the ltrim() function to remove the first character “H” from the string. Finally, you display the modified string using the echo statement.

Method 3: Using substr_replace() function

The substr_replace() function is used to replace a part of a string with another string. In order to remove the first character of a string using substr_replace(), you need to specify the start position as 0, which will start the replacement from the first character onwards. Here is an example of using the substr_replace() function to remove the first character from a string:

$string = "Hello World!";
$newString = substr_replace($string, "", 0, 1);
echo $newString; // Output: "ello World!"

In this example, you have assigned the string “Hello World!” to the variable $string. Then you used the substr_replace() function to replace the first character of the string with an empty string. The start position is set to 0, which is the beginning of the string, and the length is set to 1, which is the number of characters to be replaced. The result is then assigned to the variable $newString, which is then printed to the screen using the echo statement.

Conclusion

In PHP, removing the first character from a string is a simple task that can be accomplished using either the substr() function or the substr_replace() or ltrim() function. These functions have their own syntax and parameters that can be used to remove the first character from a string. The choice of which function to use depends on the specific requirements of the task at hand.

Recommended PHP Tutorials

  1. Remove Specific/Special Characters From String In PHP
  2. How to Replace First and Last Character From String PHP
  3. PHP Count Specific Characters in String
  4. Reverse String in PHP

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *