The same thing can be seen in the output. In the string assigned we are displaying newlines as part of the sentence for testing purposes. In the above example, we are showcasing to see the output without the use of the trim() function. Given below are the examples of trim() in PHP: Example #1 – To remove the newline character. rtrim(): This is used to trim characters from the end of the input string.ltrim(): This is used to trim characters from the beginning of the input string.trim(): This is a normal one and can be used to trim from both the beginning and end of a string.There are 3 different types of trim() functions that can be used for the below-mentioned purposes: Return values: Outputs a trimmed string.With this, we can also specify a wide range of characters. All the characters mentioned inside the braces will be trimmed. character_mask: This is an optional field that can also be specified using this parameter.str: This is the input string that will be trimmed of whitespaces.“\x0B” (ASCII 11 (0x0B)) which represents a vertical tab.“\0” (ASCII 0 (0x00)) which is called the NULL byte.“\r” (ASCII 13 (0x0D)) which is a carriage return character.“\n” (ASCII 10 (0x0A)) which represents a new line character.“\t” (ASCII 9 (0x09)) which is a tab character.” ” (ASCII 32 (0x20)) which is a normal space character.Hence without the second parameter, the trim function trims the below parameters by default: The second parameter in the syntax is optional and is used to define explicitly what characters should be trimmed exactly. In summary, if you wanted to see how to remove non-printable characters from strings in PHP, I hope these examples are helpful.Trim ( string $str ) : stringĪs shown above, input parameters for this trim function include the input string which will be trimmed of its whitespaces from the beginning and the end of its string $str. $result = preg_replace('/]/', "", $string) Summary , " ' ? ! :Īs shown in my earlier example, you actually need to use two brackets with these regex patterns when using preg_replace: Any alphanumeric character 0 to 9 OR A to Z or a to z. As the “range” name implies, these patterns can be used to match ranges of characters in PHP strings: More PHP regular expressionsįinally, while I’m in the neighborhood, here’s a list of PHP “range” regular expressions from the php.net regex page. If you do need to handle Unicode characters, this SO page shows a possible solution. More solutions (Unicode)Īs I mentioned, I don’t currently have to concern myself with Unicode characters, so the original ASCII character solution I showed works fine for me. Note that if you just echo out the original string, it prints fine: I don’t know why that regex ends up putting ? characters in the resulting string, so at the moment I’m calling this a “possible solution” rather than a solution. Per the PHP regex doc, the regex stands for “any printable character,” so for my example I thought it would leave the ‘ and ’ characters in the resulting string, but to my surprise the output looks like this: ( This page is a good resource for basic octal and hex values.) Possible solution: Use the 'print' regexĪnother possible solution is to use the ‘print’ regular expression shown in this example with preg_replace: I just tested that on my example and it worked fine, but I haven’t tested it with other strings. ![]() ![]() Note: You can read more about hex and octal character sequences on this php.net page.Īlso note that if you prefer octal characters to hexadecimal characters, this code should work as well: Php > $result = preg_replace('//', '', $string) Īs you can see, the characters ‘ and ’ are not in the $result string. In this example I just want to get rid of the characters ‘ and ’, which don’t work well in my current application: You can see how this works in the interactive PHP shell. That code removes any characters in the hex ranges 0-31 and 128-255, leaving only the hex characters 32-127 in the resulting string, which I call $result in this example. That can be done with this preg_replace code: Solution: Allow only ASCII charactersįor my purposes I don’t have to work with Unicode characters, so one of the best solutions for my purposes is to strip all non-ASCII characters from the input string. I don’t know of any built-in PHP functions to remove all non-printable characters from a string, so the solution is to use the preg_replace function with an appropriate regular expression. PHP FAQ: How do I remove all non-printable characters from a string in PHP?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |