Чтобы удалить пробелы из строки в PHP, вы можете использовать несколько методов. Вот несколько примеров:
-
Использование str_replace():
$string = "Remove whitespace from string"; $string = str_replace(' ', '', $string); echo $string;Вывод: «Удалить пробелы из строки»
-
Использование preg_replace():
$string = "Remove whitespace from string"; $string = preg_replace('/\s+/', '', $string); echo $string;Вывод: «Удалить пробелы из строки»
-
Использование функции обрезки():
$string = " Remove whitespace from string "; $string = trim($string); echo $string;Вывод: «Удалить пробелы из строки»
-
Использование ltrim() и rtrim():
$string = " Remove whitespace from string "; $string = ltrim($string); $string = rtrim($string); echo $string;Вывод: «Удалить пробелы из строки»
-
Использование str_replace() с регулярным выражением:
$string = "Remove whitespace from string"; $string = preg_replace('/\s/', '', $string); echo $string;Вывод: «Удалить пробелы из строки»