What PHP functions can be used to remove leading and trailing spaces in a string?

Leading and trailing spaces in a string can be removed using the trim() function in PHP. This function removes any whitespace characters (spaces, tabs, newlines) from the beginning and end of a string. By using trim(), you can ensure that your string does not have any unnecessary spaces that could affect its display or processing.

$string = "   Hello, World!   ";
$trimmed_string = trim($string);
echo $trimmed_string; // Output: "Hello, World!"