PHP TUTORIAL
PHP Remove white space
In many cases you want to remove white spaces. A practical example is a php form where you may want to remove the white spaces before you may want to compare the results. Here are some of the options available to you.
1. Use trim() to remove the whitespace at the beginning or at the end of a string. See the example below.
|
2. If you are need to remove the whitespace only at the begining of the string you may want to use ltrim() or rtrim() method. See the two examples below.
|
|
3. If you want to replace multiple white spaces that may lie at the beginning, end of middle, you can use the following string replacement metthod.
$str = str_replace(' ','',$str);
Check the example below
|
4. You can use the following regular expression to finy any whitespace like new line, tab, space .
preg_replace( '/\s+/', '', $str1 ); // replace all white spaces in the string $str1
The example below shows the usage of this .
|