Hướng dẫn replace whitespace php

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?

Hướng dẫn replace whitespace php

asked Jan 21, 2010 at 13:02

streetparadestreetparade

31.1k36 gold badges100 silver badges123 bronze badges

4

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).

PaulH

2,8321 gold badge14 silver badges31 bronze badges

answered Jan 21, 2010 at 13:04

Mark ByersMark Byers

781k184 gold badges1551 silver badges1440 bronze badges

11

If you want to remove all whitespace:

$str = preg_replace('/\s+/', '', $str);

See the 5th example on the preg_replace documentation. (Note I originally copied that here.)

Edit: commenters pointed out, and are correct, that str_replace is better than preg_replace if you really just want to remove the space character. The reason to use preg_replace would be to remove all whitespace (including tabs, etc.).

answered Jan 21, 2010 at 13:05

8

If you know the white space is only due to spaces, you can use:

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

But if it could be due to space, tab...you can use:

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

answered Jan 21, 2010 at 13:05

codaddictcodaddict

434k80 gold badges487 silver badges524 bronze badges

1

str_replace will do the trick thusly

$new_str = str_replace(' ', '', $old_str);

answered Jan 21, 2010 at 13:05

Hướng dẫn replace whitespace php

David HeggieDavid Heggie

2,8481 gold badge24 silver badges21 bronze badges

1

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

Nội dung chính

  • Answer: Use the PHP str_replace() Function
  • Related FAQ
  • How do I remove extra spaces from a string?
  • What is whitespace in PHP?
  • Does PHP care about whitespace?
  • How do you strip whitespace or other characters from the beginning and end of a string?

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

The output should be "thisismystring"

How can I do that?

asked Jan 21, 2010 at 13:02

streetparadestreetparade

31.1k36 gold badges99 silver badges123 bronze badges

4

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).

PaulH

2,8321 gold badge14 silver badges31 bronze badges

answered Jan 21, 2010 at 13:04

Mark ByersMark Byers

780k183 gold badges1551 silver badges1440 bronze badges

11

If you want to remove all whitespace:

$str = preg_replace('/\s+/', '', $str);

See the 5th example on the preg_replace documentation. (Note I originally copied that here.)

Edit: commenters pointed out, and are correct, that str_replace is better than preg_replace if you really just want to remove the space character. The reason to use preg_replace would be to remove all whitespace (including tabs, etc.).

answered Jan 21, 2010 at 13:05

8

If you know the white space is only due to spaces, you can use:

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

But if it could be due to space, tab...you can use:

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

answered Jan 21, 2010 at 13:05

codaddictcodaddict

434k80 gold badges487 silver badges524 bronze badges

1

str_replace will do the trick thusly

$new_str = str_replace(' ', '', $old_str);

answered Jan 21, 2010 at 13:05

David HeggieDavid Heggie

2,8481 gold badge24 silver badges21 bronze badges

1

Topic: PHP / MySQLPrev|Next

Answer: Use the PHP str_replace() Function

You can simply use the PHP str_replace() function to strip or remove all spaces inside a string.

Let's take a look at the following example to see how it actually works:

<?php
$str = 'This is a simple piece of text.';
$new_str = str_replace(' ', '', $str);
echo $new_str; // Outputs: Thisisasimplepieceoftext.
?>

The above example will however only remove spaces. If you want to remove all whitespaces including tabs, newlines, etc. you can use the preg_replace() function which perform a regular expression search and replace, as demonstrated in the following example:

<?php
$str = "This is a simple \npiece\tof text.";
$new_str = preg_replace("/\s+/", "", $str);
echo $new_str; // Outputs: Thisisasimplepieceoftext.
?>

In the above example \t represents the tab character, whereas \n represents the newline character.

To learn more about regular expression, see the tutorial on PHP regular expressions.


Here are some more FAQ related to this topic:

  • How to extract substring from a string in PHP
  • How to combine two strings in PHP
  • How to convert a string to uppercase in PHP

How do I remove extra spaces from a string?

Remove extra spaces from a string..

Remove spaces from a given string..

Move spaces to front of string in single traversal..

Remove extra spaces from a string..

URLify a given string (Replace spaces with %20).

Print all possible strings that can be made by placing spaces..

Put spaces between words starting with capital letters..

What is whitespace in PHP?

More Detail. The ctype_space() function in PHP check for whitespace character(s). It returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.

Does PHP care about whitespace?

Because PHP does not care about whitespace, you can put as many spaces or line breaks within your quotes as you would like.

How do you strip whitespace or other characters from the beginning and end of a string?

Use the . lstrip() method to remove whitespace and characters only from the beginning of a string. Use the . rstrip() method to remove whitespace and characters only from the end of a string.