Home php How to remove spaces from a string in PHP?

How to remove spaces from a string in PHP?

Author

Date

Category

How to remove spaces from a string, for example from:

16:15

do

16:15


Answer 1, authority 100%

like this str_replace ('', '', '16: 15 ');


Answer 2, authority 50%

What is really there ..

$ text = '16: 15 ';
$ chars = preg_split ('// u', $ text, null, PREG_SPLIT_NO_EMPTY);
$ resultChars = array_filter ($ chars, function ($ char) {
  return! preg_match ('/ / u', $ char);
});
$ result = implode ('', $ resultChars);
var_dump ($ result); // string (5) "16:15"

From the Bad Advice series


Answer 3, authority 40%

$ text = "16: 15";
$ result = preg_replace ("/ \ s + /", "", $ text);
echo $ result;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions