PHP trim() function: Remove characters from string

In this tutorial, we look at the PHP trim function. We look at how you can remove whitespaces and characters from a string using the trim function in PHP.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around o…

In this tutorial, we look at the PHP trim function. We look at how you can remove whitespaces and characters from a string using the trim function in PHP.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.



Table of Contents – PHP Trim:



What does the PHP trim() function do?

The trim() function in PHP removes whitespace or any other predefined character from both the left and right sides of a string.

ltrim() and rtrim() are used to remove these whitespaces or other characters from the left and right sides of the string. However, making use of just the trim() function without specifying ‘l’ or ‘r’ removes characters from both sides.



Code & Explanation:

In this section, we look at the various syntax, parameters, and return values used in the trim() function. Post that we look at a code snippet using the trim() function.



Syntax:

trim($string, $charlist)



Parameters

  • $string – Required. This is the string or the variable containing the string from which you want to remove whitespaces or characters.
  • $charlist – Optional. This parameter specifies the character that needs to be removed from the string. If left empty, all the characters mentioned below would be removed.

    • “\0” – NULL
    • “\t” – tab
    • “\n” – newline
    • “\x0B” – vertical tab
    • “\r” – carriage return
    • ” ” – ordinary white space
      ### Return Value:
      A modified string with whitespaces or the specified characters removed from both sides is returned.



Code using PHP Trim:

<?php
// PHP program using trim() 

$str = "  Hire freelance developer ";

// Since a second parameter was not passed
// leading and trailing whitespaces are removed
echo trim($str);
?>

The output for the above code snippet would be as follows:

Hire freelance developer

Now let’s look at a case where we pass a second argument.

<?php
// removing the predefined character 
$str = "Hire freelance developer";
echo trim($str, "Hir");
?>

The output for this code snippet would be the following:

e freelance develope

As you can see the “hir” from “Hire” and the “r” from “developer” were removed.



Closing thoughts – PHP trim():

There are no caveats as such while using the PHP trim() function. I would recommend practicing the trim() method as it can get tricky at times. Also, try using the ltrim() and rtrim() both these functions have very specific use cases.


Print Share Comment Cite Upload Translate
APA
hrishikesh1990 | Sciencx (2024-03-28T13:18:49+00:00) » PHP trim() function: Remove characters from string. Retrieved from https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/.
MLA
" » PHP trim() function: Remove characters from string." hrishikesh1990 | Sciencx - Monday September 20, 2021, https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/
HARVARD
hrishikesh1990 | Sciencx Monday September 20, 2021 » PHP trim() function: Remove characters from string., viewed 2024-03-28T13:18:49+00:00,<https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/>
VANCOUVER
hrishikesh1990 | Sciencx - » PHP trim() function: Remove characters from string. [Internet]. [Accessed 2024-03-28T13:18:49+00:00]. Available from: https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/
CHICAGO
" » PHP trim() function: Remove characters from string." hrishikesh1990 | Sciencx - Accessed 2024-03-28T13:18:49+00:00. https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/
IEEE
" » PHP trim() function: Remove characters from string." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/. [Accessed: 2024-03-28T13:18:49+00:00]
rf:citation
» PHP trim() function: Remove characters from string | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/09/20/php-trim-function-remove-characters-from-string/ | 2024-03-28T13:18:49+00:00
https://github.com/addpipe/simple-recorderjs-demo