Replace First and Last Character From String PHP

Replace First and Last Character From String PHP

To replace the first and last character from a string PHP. This tutorial teaches to you, how you can easily replace first and last character string form with example.

In PHP, there may be situations where you need to replace the first and last character of a string. This is a common task when working with strings and can be accomplished easily using built-in PHP functions. In this article, you will learn how to replace the first and last characters of a string in PHP.

Before we dive into the solution, let’s first understand the problem we are trying to solve. Suppose we have a string, “Hello World!”, and we want to replace the first character, “H”, with “J” and the last character, “!”, with “?”. The resulting string should be “Jello World?”.

We can use the substr_replace function in PHP to replace first or last character from string in PHP.

To Replace character from string PHP

Before we take an example to replace first character from string and replace last character from a string. We will explain you substr_replace() in PHP.

substr_replace() Function PHP

The PHP substr_replace() function is used to replace a part of a string with another string.

Syntax

substr_replace($str, $replacement, $start, $length)

Parameters

The substr_replace() function is accept only 4 parameters. And the first three parameters are required and remember the last parameter is optional. Below we have described all parameters of substr_replace().

  • $str: This parameter is required. In this parameter, we will put original string.
  • $replacement: This parameter is also required. In this parameter, we will put other string we want to replace with the original string.
  • $start: This parameter is also required.
    • When we put a positive number in $start, replacement starts at the specified position in the string.
    • When we put a negative number in $start, replacement starts at the specified position from the end of the string.
    • If $start is 0, replacement occurs from the first character of the string.
  • $length: It’s last parameter and optional.
    • If $ length is positive, it represents the length of the part of the $ string that is to be replaced.
    • If $ length is negative, it represents the number of characters before the end of the $ string from which the first substitution needs to be stopped.
    • If $ length is 0, then insertion is done instead of substitution.

Replace first character from string PHP

Suppose we have one string like this “Hello Developers”. In this string we want to replace first five characters in PHP. So you can use the substr_replace() function like this:

<?php 

 $a_string = "Hello Developers";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . substr_replace($string, "Hi", 0, 5) . "\n"; 

?> 

We can now replace the first character with the new character using the str_replace() function. The str_replace() function takes three parameters: the string to search for, the string to replace with, and the string to search in. In our case, we want to search for the first character ($a_string) and replace it with the new character (“Hi”).

Replace last character from string PHP

Suppose we have one string like this “Hello World” . In this string we want to replace last five characters in PHP. So you can use the substr_replace() function like this:

<?php 

 $string = "Hello World";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . substr_replace($string ,"dev",-5) . "\n";

?> 

Finally, we can replace the last character with the new character using the str_replace() function again.

And that’s it! We have successfully replaced the first and last characters of a string in PHP.

Conclusion

Replacing the first and last characters of a string in PHP is a simple task that can be accomplished using built-in PHP functions. By following the steps outlined in this article, you can easily modify a string to fit your needs.

Recommended PHP Tutorials

  1. Functions: Remove First Character From String PHP
  2. Remove Specific/Special Characters From String In PHP
  3. PHP Count Specific Characters in String
  4. Reverse String in PHP

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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