This led to data inconsistency and lots of headaches for Jane. However, she faced a recurring problem: her users often submitted data with extra spaces or other unwanted characters. Jane had a knack for coding and loved creating web applications. Once upon a time, in a galaxy not so far away, there was a PHP developer named Jane. Think of us as your Sherpa, guiding you through the mountainous terrain of PHP string wrangling. Our mission is to help you harness the full power of the trim function and elevate your string manipulation game to new heights. Well, whether you're a seasoned PHP developer or just starting your journey in the world of scripting, we've got you covered. You might be wondering who this article is meant for. It's like having a virtual barber for your strings-snipping away the unwanted characters and making them look presentable. Remember the last time you had to deal with a pesky typo or extra spaces in a user's input, and it ended up ruining your otherwise perfect data validation? Fret no more! PHP's trim function is here to save your day and your sanity. So, buckle up and let's embark on this text-taming journey together. In this article, we're going to dive deep into the PHP trim function-a function so versatile that it can be compared to a Swiss Army knife for strings. If you're looking to trim the fat (or the excess spaces, tabs, and other characters) from your strings, you've come to the right place. Welcome, dear reader, to the wild and wonderful world of PHP string manipulation. For more information, read our affiliate disclosure. If you click an affiliate link and subsequently make a purchase, we will earn a small commission at no additional cost to you (you pay nothing extra). In summary, if you wanted to see how to remove non-printable characters from strings in PHP, I hope these examples are helpful.Important disclosure: we're proud affiliates of some tools mentioned in this guide. $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 |