Good day! There is text in win1252 that contains symbols that shows like 0x9D
in Sublime text editor.
Help please with advice how it possible to find and replace all of them using PHP?
Good day! There is text in win1252 that contains symbols that shows like 0x9D
in Sublime text editor.
Help please with advice how it possible to find and replace all of them using PHP?
Use preg_replace function.
preg_replace('/\x9D/', '', $input);
If source code in cp1252 encoding.
Add u
modifier if source string is in utf-8.
If you want to replace all non-ASCII characters (bytes) in a file (that are all with the most significant 8th bit set), you can create a character class in PCRE to match all of those: [\x80-\xFF]
. Compare SQUARE BRACKETS AND CHARACTER CLASSES and BACKSLASH.
You can then replace all of them with $result =
preg_replace
('~[\x80-\xFF]~', 'your replacement string', $subject);
. If you need a conditional replacement based on the actual byte value, see preg_replace_callback
()
for replacing with a callback function.
You may also want to replace DEL
so you can start the class at \x7F
instead of \x80
.
For Q&A material on how to apply this on a file, please see PHP preg_replace in a file?.