![]() ![]() ![]() The only problem now is that the main string might contain multiple whitespace characters at both ends. In the above example, the after \s means that we want to replace one or more whitespace characters with a single space. Output - string(54) "This string contains many whitespace characters in it." $stripped = preg_replace('/\s /', ' ', $sentence) This is different than removing all the spaces so we will have to make small changes in our regular expression and the preg_replace() function. Most of the times when you decide to remove extra whitespace characters from a string, you would want to replace two or more of them with a single space character. Var_dump($stripped) Replace multiple whitespace characters with a single space Output - string(44) "Thisstringcontainsaspaceandatabatbothends." $stripped = preg_replace('/\s/', '', $sentence) So, we will be using \s to represent all the whitespace that we want to go away. The special \s character in a regular expression is used to represent all whitespace characters that we removed by trim(), ltrim() and rtrim(). You will have to use something more sophisticated like regular expressions. In such cases, using the str_replace() function won’t cut it. It is important to remember that whitespace can consist of more than just space characters. Output - string(44) "Thisstringcontainsaspaceandatabatbothends. $stripped = str_replace(' ', '', $sentence) If you just want to remove all the whitespace characters irrespective of where they occur in the string, you should simply use str_replace() to replace all their occurrences with a blank string. The trimming function we have discussed so far will be ineffective against it. Some times, the strings you are working with will have unwanted whitespace in the middle and/or the beginning as well as the end. Var_dump($stripped) Remove all the whitespace in a String Output - string(52) "This string contains a space and a tab at both ends." PHP $sentence = " This string contains a space and a tab at both ends. Just like ltrim() and rtrim() this will remove the following characters: " " an ordinary space, "\t" a tab, "\n" a new line (line feed), "\r" a carriage return, "\0" the NUL-byte and "\x0B" a vertical tab. If you want to remove whitespace from both ends of a string, you should use the trim() function instead of using both ltrim() and rtrim(). Strip or remove whitespace from both ends of a String The whitespace in the beginning stays intact. Output - string(55) " This string contains a space and a tab at both ends."Īs you can see in the above example, the rtrim() function only removed the whitespace from the end. Output - string(57) " This string contains a space and a tab at both ends. All the characters stripped by rtrim() are: " " an ordinary space, "\t" a tab, "\n" a new line, "\r" a carriage return, "\0" the NULL-byte and "\x0B" a vertical tab. This function works exactly like ltrim() but removes whitespace from the end. You can use the PHP rtrim() function to remove all the whitespace characters from the end of a string. Strip or remove whitespace from the end of a String Another advantage of using ltrim() is that it is not very complicated to use and faster than using regular expressions. For instance, you could also use this function to remove. One good thing about ltrim() is that it allows you to specify some other problematic characters that you want to remove from the left end of any string. Output - string(56) "This string contains a space and a tab in the beginning." PHP $sentence = " This string contains a space and a tab in the beginning." This function will remove the following whitespace characters: " " (an ordinary space), "\t" (a tab), "\n" (a new line), "\r" (a carriage return), "\0" (the NUL-byte) and "\x0B" (a vertical tab). If you want to remove whitespace only from the beginning of a string, you should use the ltrim() function in PHP. Strip or remove whitespace from the beginning of a String ![]() Replace multiple whitespace characters with a single space.Strip or remove whitespace from both ends of a String.Strip or remove whitespace from the end of a String.Strip or remove whitespace from the beginning of a String. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |