How can PHP developers ensure proper data sanitization when converting spaces in strings?
When converting spaces in strings, PHP developers can ensure proper data sanitization by using the `trim()` function to remove any leading or trailing spaces, and the `preg_replace()` function with a regular expression to replace multiple spaces with a single space.
// Sample string with extra spaces
$string = " Hello World ";
// Remove leading and trailing spaces
$string = trim($string);
// Replace multiple spaces with a single space
$string = preg_replace('/\s+/', ' ', $string);
// Output sanitized string
echo $string;