Search in text by ASCII hex code 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?

Answer

Solution:

Use preg_replace function.

preg_replace('/\x9D/', '', $input);

If source code in cp1252 encoding. Add u modifier if source string is in utf-8.

Answer

Solution:

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 = ('~[\x80-\xFF]~', 'your replacement string', $subject);. If you need a conditional replacement based on the actual byte value, see () 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 .

Source