How to delete all spaces in string php?
Topic: PHP / MySQLPrev|Next Show Answer: Use the PHP str_replace() FunctionYou can simply use the PHP Let's take a look at the following example to see how it actually works:
The above example will however only remove spaces. If you want to remove all whitespaces including tabs, newlines, etc. you can use the
In the above example To learn more about regular expression, see the tutorial on PHP regular expressions. Related FAQHere are some more FAQ related to this topic:
How can I strip / remove all spaces of a string in PHP? I have a string like The output should be 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:
For all whitespace (including tabs and line ends), use preg_replace:
(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:
See the 5th example on the preg_replace documentation. (Note I originally copied that here.) Edit: commenters pointed out, and are correct, that
answered Jan 21, 2010 at 13:05
8 If you know the white space is only due to spaces, you can use:
But if it could be due to space, tab...you can use:
answered Jan 21, 2010 at 13:05
codaddictcodaddict 434k80 gold badges487 silver badges524 bronze badges 1 str_replace will do the trick thusly
answered Jan 21, 2010 at 13:05
David HeggieDavid Heggie 2,8481 gold badge24 silver badges21 bronze badges 1 How do I remove all spaces from a string?The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
How do I trim a space in PHP?The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.
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.
Do spaces matter in PHP?Whitespaces are generally ignored in PHP syntax, but there are several places where you cannot put them without affecting the sense (and the result).
|