trim()

Removes specified characters from the beginning and end of a string.

Table of contents

  1. Signature
  2. Parameters
  3. Returns
  4. Description
  5. Examples
    1. Basic Usage
    2. Custom Characters
    3. Whitespace Handling
    4. URL Path Cleaning
  6. Use Cases
    1. Form Input Cleaning
    2. CSV Parsing
    3. Path Normalisation
    4. Log Message Cleaning
  7. Comparison with PHP trim()
  8. Related Methods

Signature

public static function trim(string $string, string $characters = " \t\n\r\0\x0B"): string

Parameters

Parameter Type Default Description
$string string - The input string to trim
$characters string " \t\n\r\0\x0B" Characters to remove from both ends

Returns

string - The trimmed string.


Description

The trim() method removes specified characters from both the beginning and end of a string. By default, it removes common whitespace characters:

Character Description
` ` Space
\t Tab
\n Newline (line feed)
\r Carriage return
\0 Null byte
\x0B Vertical tab

This method provides more explicit control over character removal compared to PHP’s built-in trim().


Examples

Basic Usage

use MarjovanLier\StringManipulation\StringManipulation;

$result = StringManipulation::trim('  Hello World  ');
echo $result; // Output: Hello World

Custom Characters

// Remove specific characters
$result = StringManipulation::trim('###Hello###', '#');
echo $result; // Output: Hello

// Remove multiple custom characters
$result = StringManipulation::trim('***Hello***', '*#');
echo $result; // Output: Hello

Whitespace Handling

// Tabs and newlines
$input = "\t\nHello World\n\t";
$result = StringManipulation::trim($input);
echo $result; // Output: Hello World

// Mixed whitespace
$input = "  \t  Hello  \n  ";
$result = StringManipulation::trim($input);
echo $result; // Output: Hello

URL Path Cleaning

$path = '/api/users/';
$result = StringManipulation::trim($path, '/');
echo $result; // Output: api/users

Use Cases

Form Input Cleaning

function cleanFormInput(array $input): array
{
    return array_map(function ($value) {
        if (is_string($value)) {
            return StringManipulation::trim($value);
        }
        return $value;
    }, $input);
}

$input = [
    'name' => '  John Doe  ',
    'email' => '  john@example.com  ',
];

$cleaned = cleanFormInput($input);
// Result: ['name' => 'John Doe', 'email' => 'john@example.com']

CSV Parsing

function parseCSVLine(string $line): array
{
    $fields = explode(',', $line);

    return array_map(function ($field) {
        // Remove quotes and whitespace
        return StringManipulation::trim($field, " \t\n\r\"'");
    }, $fields);
}

$line = '"John" , "Doe" , "john@example.com"';
$fields = parseCSVLine($line);
// Result: ['John', 'Doe', 'john@example.com']

Path Normalisation

function normalisePath(string $path): string
{
    // Remove trailing slashes
    $path = StringManipulation::trim($path, '/\\');

    // Ensure leading slash
    return '/' . $path;
}

$path = normalisePath('/api/users//');
// Result: /api/users

Log Message Cleaning

function cleanLogMessage(string $message): string
{
    // Remove control characters and excessive whitespace
    return StringManipulation::trim($message, " \t\n\r\0\x0B\x1B");
}

Comparison with PHP trim()

Feature StringManipulation::trim() PHP trim()
Type safety Explicit string parameter Mixed input
Return type Guaranteed string String (with type juggling)
Default chars Same whitespace set Same whitespace set
Custom chars Second parameter Second parameter


Back to top

Copyright © 2024 Marjo Wenzel van Lier. Distributed under the MIT License.