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

16:15

to make

16:15

  • 6
    And how did you try to do it yourself? - VenZell
  • one
    Come on, a person at least does not ask for распарсить сайт or отправить email с вложением , etc. - Naumov
  • Wash everything easier. The author certainly when the output does: <?php echo $h; ?> : <$php echo $m; ?> <?php echo $h; ?> : <$php echo $m; ?> <?php echo $h; ?> : <$php echo $m; ?> a need: <?php echo $h; ?>:<$php echo $m; ?> <?php echo $h; ?>:<$php echo $m; ?> <?php echo $h; ?>:<$php echo $m; ?> / <?php printf("%s:%s", $h, $m); ?> <?php printf("%s:%s", $h, $m); ?> ... - E_p
  • Maybe trim() ? - Eugen Eray

3 answers 3

so str_replace(' ', '', '16 : 15');

    What is already 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 series "Bad advice"

    • : D upvote for a sense of humor - Naumov
     $text = "16 : 15"; $result = preg_replace("/\s+/", "", $text); echo $result; 
    • From the cannon on the sparrows ... Regulars are not needed here. - AK
    • @AK aha famously, the author could not resist the downvote - Naumov
    • @AK, you should always keep in mind not only the author of the question, but also those who can later follow the link. And if for the author it is from a cannon on sparrows, then the other will need to remove any whitespace characters, not just spaces - Ipatiev
    • @Ipatiev Then, probably, another question will get into, more appropriate - but even if that is the case, then it would be good to describe your decision with a couple of phrases. - AK