PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP trim() Function: Remove Spaces On Both Sides Of A String

The trim() function in PHP is used to remove whitespaces or other predefined characters from both sides of a string. This is especially useful when handling user input, as users often accidentally include extra spaces.

Here's the basic syntax of the trim() function:

trim(string, character_mask)
  • string: The input string that you want to trim.
  • character_mask (optional): If this parameter is provided, the characters in it will be stripped from the beginning and end of the string. It defaults to whitespace characters if not provided.

Let's look at a few examples:

Example 1: Removing whitespace

$str = "   Hello, World!   ";
$trimmed_str = trim($str);

echo $trimmed_str; // Outputs: "Hello, World!"

In the above example, trim() removes the spaces at the beginning and end of the string.

Example 2: Removing specific characters

$str = "Hello, World!";
$trimmed_str = trim($str, "Hdle");

echo $trimmed_str; // Outputs: "o, Wor"

In the above example, trim() removes the characters "H", "d", "l", and "e" from the beginning and end of the string. Note that "l" and "e" in the middle of "Hello" and "World" are not removed, because trim() only affects the beginning and end of the string.

In addition to trim(), PHP also provides the ltrim() and rtrim() functions to remove characters from the left side (beginning) or right side (end) of a string, respectively.

  1. How to use trim() in PHP:

    trim() is used to remove whitespace or other characters from the beginning and end of a string. Here's a basic example:

    <?php
    $originalString = "   Hello, World!   ";
    $trimmedString = trim($originalString);
    
    echo $trimmedString;
    
  2. Removing leading and trailing spaces in PHP:

    <?php
    $text = "   This is a string with spaces.   ";
    $trimmedText = trim($text);
    
    echo $trimmedText;
    
  3. Trim whitespace from a string in PHP:

    <?php
    $text = "  Trim leading and trailing spaces.  ";
    $trimmedText = trim($text);
    
    echo $trimmedText;
    
  4. PHP trim() function with character list:

    <?php
    $text = " #!Trim this string!# ";
    $trimmedText = trim($text, "#!");
    
    echo $trimmedText;
    
  5. Removing newline characters with PHP trim():

    <?php
    $text = "Hello,\nWorld!";
    $trimmedText = trim($text);
    
    echo $trimmedText;
    
  6. Strip whitespaces from a variable in PHP:

    <?php
    $variableWithSpaces = "  Variable with spaces.  ";
    $trimmedVariable = trim($variableWithSpaces);
    
    echo $trimmedVariable;
    
  7. Trim specific characters from a string in PHP:

    <?php
    $text = "Remove special characters: #@! from this string";
    $trimmedText = trim($text, '#@! ');
    
    echo $trimmedText;
    
  8. Strip HTML tags and trim spaces in PHP:

    <?php
    $htmlString = "<p>   Trim spaces and remove HTML tags.   </p>";
    $trimmedText = trim(strip_tags($htmlString));
    
    echo $trimmedText;
    
  9. Using trim() for form input validation in PHP:

    <?php
    $username = $_POST['username'];
    $trimmedUsername = trim($username);
    
    // Use $trimmedUsername for validation or further processing
    
  10. PHP trim() function with UTF-8 support:

    For UTF-8 support, use mb_trim() from the mbstring extension:

    <?php
    $text = "   Trim UTF-8 string.   ";
    $trimmedText = mb_trim($text, 'UTF-8');
    
    echo $trimmedText;
    
  11. Handling whitespaces in PHP strings:

    Use trim() to handle leading and trailing whitespaces. For internal spaces, consider other functions like str_replace().