Skip to content
Fix Code Error

How do I strip all spaces out of a string in PHP?

March 13, 2021 by Code Error
Posted By: Anonymous

How can I strip / remove all spaces of a string in PHP?

I have a string like $string = "this is my string";

The output should be "thisismystring"

How can I do that?

Solution

Do you just mean spaces or all whitespace?

For just spaces, use str_replace:

$string = str_replace(' ', '', $string);

For all whitespace (including tabs and line ends), use preg_replace:

$string = preg_replace('/s+/', '', $string);

(From here).

Answered By: Anonymous

Related Articles

  • PHP: How to remove all non printable characters in a string?
  • Stacked Tabs in Bootstrap 3
  • Active tab issue on page load HTML
  • Reference — What does this symbol mean in PHP?
  • How can I make this v-tabs Vuetify.js component work?
  • Vue rendering order
  • vue js cant understand keep alive
  • Dynamic tabs with user-click chosen components
  • Vaadin: Tabs are displayed in a really bizarre way - is this…
  • Vuejs: Using keep-alive (Or something similar) on a slot

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How can I connect to Android with ADB over TCP?

Next Post:

Convert array to JSON

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error