How to remove spaces from string in PHP. In this tutorial i’ll explain how to remove spaces from string using regex as well as using in-built function in PHP.
If you know regex then it’s good otherwise you can refer my previous tutorial on Regular expression.
How to Remove Spaces from String in PHP
Let’s create a simple regex which remove whitespace from string.
Regex Symbols and their Meanings
\s = Matches any whitespace character (spaces, tabs, line breaks).
+ = Match one or more preceding token
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $strVal = " Remove whitespaces from string "; /* Print string length before removing white spaces */ var_dump ($strVal); $strVal = preg_replace('/\s+/','',$strVal); /* Print string length after removing whitespace */ var_dump ($strVal); // string(27) "Removewhitespacesfromstring" |
How to Remove WhiteSpace from the Beginning of String in PHP
To remove spaces at the beginning of a string
^\s+ – Remove whitespace at the beginning of a string
^ – start at the beginning
How to upload multiple files in PHP.
1 2 3 4 5 6 |
<?php $strVal = " Test string for space "; $strVal = preg_replace('/^\s+/','',$strVal); echo $strVal; |
ltrim() – ltrim() strip whitespace or other characters from the beginning of string.
Syntax –
1 |
ltrim ( string $str [, string $character_mask ] ) |
If you don’t specify second parameter, then it Strip whitespace from the beginning of a string.
1 2 3 4 5 6 7 |
<?php $strVal = " Remove whitespaces from string "; var_dump(ltrim ($strVal)); /* output */ string(32) "Remove whitespaces from string " |
How to Remove WhiteSpace from the End of String in PHP
METHOD 1 – Using Regex.
\s+$ – Remove whitespace at the end of a string
\s – Matches any whitespace character
+ – Match one or more preceding token
$ – Matches the end of a string
1 2 3 4 5 6 7 |
<?php $strVal = " Test string for space "; $strVal = preg_replace('/\s+$/','',$strVal); var_dump($strVal); // string(24) " Test string for space" |
METHOD 2 – Using PHP in-built function rtrim().
rtrim() – Strip whitespace (or other characters) from the end of a string.
1 |
rtrim ( string $str [, string $character_mask ] ) |
If you don’t specify second parameter, then it Strip whitespace from the end of a string.
1 2 3 4 5 |
$strVal = " Test string for space "; $strVal = rtrim($strVal); var_dump($strVal); // string(24) " Test string for space" |
How to Remove Multiple Spaces in String
\s{2,} – It will check for two or more consecutive spaces
You can write, and test your regex pattern in regexr.
Let’s implement them in PHP function
1 2 3 4 5 6 |
<?php $strVal = " Test string for space "; $strVal = preg_replace('/\s{2,}/','',$strVal); var_dump($strVal); // string 'Test string for space' (length=21) |
Remove WhiteSpace from Beginning and End of String through trim() in PHP
PHP provides in-built trim() method through which you can strip whitespace from beginning and end of string.
trim(): Remove whitespaces at the beginning and end of a string.
1 2 3 4 5 6 |
<?php $strVal = " Test string for space "; $strVal = trim($strVal); var_dump($strVal); |