Avatar ·

How to remove all spaces from a string in PHP?

Hello everyone! I'm new to PHP and I've encountered a task: I need to remove all spaces from a string. For example, I have the string "Привет мир! Как дела?" and I need to get "Приветмир!Какдела?". I tried using the trim() function, but it only removes spaces at the beginning and end of the string. I tried str_replace(), but I'm not sure if I'm using it correctly. Here's my code: ```php $string = "Привет мир! Как дела?"; $result = str_replace(" ", "", $string); echo $result; // Outputs "Приветмир!Какдела?" ``` This works, but I've heard there are other types of spaces, like non-breaking spaces or tabs. What should I do if the string contains different types of spaces? Is there a more universal way? Also, another question: what if I need to remove only extra spaces but keep single spaces between words?