PHP trim
Remove surrounding whitespace before looking up a booking.
A simple example
trim
- Start with " A "
- Do this Remove surrounding spaces
- Result "A"
<?php
$value = " A ";
echo (trim($value)), PHP_EOL;
A
Syntax
trim($label)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpClean up a copied booking code
A booking number copied from a confirmation email includes whitespace around it. Prepare the value for an exact lookup without deleting characters that belong inside the number.
<?php
$label = " AB-17 ";
echo (trim($label)), PHP_EOL;
AB-17
Padding disappears while the hyphen in the middle is kept.
Clean a pasted receipt code
A cashier pastes a receipt identifier that includes stray whitespace from a spreadsheet. Clean the outer characters before using the code to retrieve the matching transaction.
<?php
$value = " AB-17 ";
echo (trim($value)), PHP_EOL;
AB-17
Both margins disappear from the copied code.
Keep the space inside a course title
A user pastes a name or code from another application. Remove the unwanted outer padding while preserving the spaces and letters that belong to the actual value.
<?php
$value = "\tData Science ";
echo (trim($value)), PHP_EOL;
Data Science
The space between the two words remains.
What to watch for
The set of removable whitespace characters differs between APIs. This example uses ordinary spaces.