Does php trim remove newline11/27/2023 Intuitively, this seems like it would be fast, but it's not always the case, you should definitely benchmark to see if it saves you anything. Preg_replace is pretty efficient, but if you're doing this operation a lot, you could build an array of chars you want to remove, and use str_replace as noted by mgutt below, e.g. You could look for and remove that specific sequence, but with the /u modifier in place, you can simply add \xA0 to the character class: $string = preg_replace('//u', '', $string) In a UTF-8 string, this would be encoded as 0xC2A0. If you're dealing with Unicode, there are potentially many non-printing elements, but let's consider a simple one: NO-BREAK SPACE (U+00A0) But it makes life easier if you want to remove other chars. Strictly speaking, this would work without the /u modifier. This works in ASCII and UTF-8 because both share the same control set range (as noted by mgutt below). If you have a UTF-8 encoded string, then the /u modifier can be used on the regex $string = preg_replace('//u', '', $string) An easy adjustment - just look for 0-31 and 127 $string = preg_replace('//', '', $string) Īh, welcome back to the 21st century. If you've got some form of 8 bit ASCII, then you might want to keep the chars in range 128-255. You fell into a Hot Tub Time Machine, and you're back in the eighties. It matches anything in range 0-31, 127-255 and removes it. If your Tardis just landed in 1963, and you just want the 7 bit printable ASCII chars, you can rip out everything from 0-31 and 127-255 with this: $string = preg_replace('//', '', $string)
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |