php remove last character
php remove last character

In PHP remove last character from string if comma

In this tutorial, we will learn in php remove last character from string if comma or any last character. We have multiple built-in functions in PHP to remove last character from string. There are the following functions which are

  • rtrim()
  • substr()
  • substr_replace()

In PHP remove last character from string if comma using rtrim():

$string = "Hello World,";
echo $string . "<br>";
echo rtrim($string,",");
Output

Syntax of rtrim():

rtrim(string,Characters)

  • The string parameter is required.
  • The second parameter is optional. If you provide a second parameter then it will remove that characters from the last of your string. Otherwise, it will check your string and remove last character from string
  1. “\0” – NULL
  2. “\t” – tab
  3. “\n” – new line
  4. “\x0B” – vertical tab
  5. “\r” – carriage return
  6. ” ” – ordinary white space

You can learn more about of rtrim function by click here.

How to remove last two characters in php using substr():

$string = "Hello world,";
echo $string;
echo "<br>";
echo substr($string,0,-2);
Output

Syntax of substr():

substr(string,start,length)

  • The string parameter is required
  • Start parameter is required. And it must be integer value. it can positive or negative value.
  1. A positive integer means it will start from start of string.
  2. Negative integer mean it will start from end of string.
  • Length parameter is optional. It define the returned string length. By Default it is to end of define string.

Lets look some other examples of substr();

echo substr("Hello world",2)."<br>";

Output: llo world

echo substr("Hello world",-2)."<br>";

Output: ld

Remove end character from string php using substr_replace():

$string = "Hello world,";
echo $string;
echo "<br>";
echo substr_replace($string ,"",-1);
Output

As you can see we checked multiple method in php remove last character. You will aslo enjoy to learn below topics.

If you want to hire a php developer please check detail

Read about In php remove value from array

Read about JQuery Multiselect Dropdown with custom icon

Read about Easily by using javascript trigger enter key

How to get first 10 characters from string in php?

You can easily get first 10 characters from string in php by using substr() function. You can use below code.
$string = “Hello how are you doing today”;
$result = substr($string, 0, 10);

How do i remove the last character of a string?

You can use multiple built-in function in php remove last character. Mostly commonly used function is substr(). Example
$string = “Hello world,”;
substr($string,0,-1);
If you want to remove last multiple characters then change -1 to your integer value.

How do i remove the first and last character of a string in php?

You can remove the first and last character of a string in php by using substr() function.
In substr() function give the second parameter integer value as length that how much you want to remove from start and in third parameter give length from end. Example
$string = “Hello how are you doing today”;
$result = substr($string, 1, -1);

Leave a Reply