How to Remove Character from String in PHP

0
181
PHP Trim Example | trim() Function In PHP Tutorial

The trim() function removes whitespace (or other characters) from the beginning and end of a string.

How to Remove a Character from String in PHP

To remove a character from a string in PHP, use the trim() function. The trim() is a built-in PHP function that removes the characters from both sides of the string.

To remove whitespace from the beginning and end of the string in PHP, use the trim() function. The trim() function returns the string with whitespace stripped from the beginning and end of the string. Without a second parameter, trim() will strip these characters.

See the following syntax of the PHP trim() function.

Syntax

trim(string, charlist)

Arguments

The string parameter is required, which specifies the string to check.

The charlist parameter is optional and specifies the characters to be removed from the string. If this is not mentioned, then all of the following characters will be removed:

  1. “\0” – NULL
  2. “\t” – tab
  3. “\n” – a new line
  4. “\x0B” – vertical tab
  5. “\r” – carriage return
  6. ” ” – ordinary white space

See the following code example.

<?php

$str = "     Demogorgons ";
echo "Without trim: " . $str;
echo "\n";
echo "With trim: " . trim($str);

See the following output.

➜  php php app.php
Without trim:      Demogorgons
With trim: Demogorgons                                                          
➜  php

How to trim array values in PHP

To trim an array in PHP, combine the trim() and array_walk() functions.

Let’s trim the array values. See the following code.

<?php

function trim_array_value(&$value) 
{
  $value = trim($value); 
}

$st = array('Mike', 'Robin', '   Eleven   ');
print_r($st);

array_walk($st, 'trim_array_value');
print_r($st);

In the above code, we have defined the function called trim_array_value(), which returns the trimmed value, and then we have created an array using the PHP array_walk() function from that value and printed it on the console. Now, see the output.

➜  php php app.php
Array
(
    [0] => Mike
    [1] => Robin
    [2] =>    Eleven
)
Array
(
    [0] => Mike
    [1] => Robin
    [2] => Eleven
)

You can see in the output the Eleven string does not have any white space.

Because the trim() trims characters from the beginning and end of the string, it may be confusing when the characters are (or are not) removed from the middle. 

The trim(‘abc’, ‘bad’) function removes both ‘a’ and ‘b’ because it trims ‘a’, thus moving ‘b’ to the beginning to be trimmed.

Please, note that trim() returns an empty string when the argument is an unset/null variable.

When specifying a character mask, make sure that you use double quotes.

<?php

$data = " 
      Eleven Or Jane   "; //here is a string with some trailing and leading whitespace

$trimmed   = trim($data, " \t\n\r");
echo $data.PHP_EOL;
echo $trimmed;

See the following output.

➜  php php app.php

      Eleven Or Jane
Eleven Or Jane                                                                  
➜  php

The second way to trim all the elements of an array

We can use the PHP array_map() function to trim all the items from the array.

<?php

$st = array('Mike', 'Robin', '   Eleven   ');
print_r($st);

$newarray = array_map('trim', $st);
print_r($newarray);

See the following output.

➜  php php app.php
Array
(
    [0] => Mike
    [1] => Robin
    [2] =>    Eleven
)
Array
(
    [0] => Mike
    [1] => Robin
    [2] => Eleven
)
➜  php

Remove multiple occurrences of whitespace characters in PHP

To remove the multiple occurrences of whitespace characters in a string, use PHP’s preg_replacE() function.

<?php

$str = "    El likes Eggos, lots of Eggos    ";
echo $str."\n";
$op = preg_replace('/\s+/', ' ', $str);
echo $op;

See the following output.

➜  php php app.php
    El likes Eggos, lots of Eggos
 El likes Eggos, lots of Eggos                                                  
➜  php

Sometimes, the strings you are working with will have unwanted whitespace in the middle, the beginning, and the end. The trimming function will be ineffective against it.

If you want to remove all the whitespace characters irrespective of where they occur in the string, you should use str_replace() to replace all their occurrences with a blank string.

$stripped = str_replace(' ', '', $sentence);

See the following example.

<?php

// app.php

$str = "    El likes Eggos, lots of Eggos    ";
echo $str."\n";
$stripped = str_replace(' ', '', $str);
echo $stripped;

See the output.

➜  php php app.php
    El likes Eggos, lots of Eggos
EllikesEggos,lotsofEggos                                                        
➜  php

How to remove the last character of string in PHP

To remove the last character of a string in PHP, use the substr() and mb_substr() methods.

<?php

$string = "Eleven like Eggos..";
echo $string . "\n";

// substr function
echo "substr: " . substr($string, 0, -1);

echo "\n";

// mb_substr multibyte version
echo "mb_substr: " . mb_substr($string, 0, -1);

echo "\n";

Okay, now see the following output.

➜  php php app.php
Eleven like Eggos..
substr: Eleven like Eggos.
mb_substr: Eleven like Eggos.
➜  php

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.