1

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?

Maksym K.
  • 11
  • 2
  • one code example? – francisco Sep 01 '22 at 11:19
  • When sublime shows `0x9D` do you have any reference at hand what that means in Sublime? Which encodings does Sublime support? And you ask about search and replace - with what do you want to replace it? – hakre Sep 05 '22 at 15:08

2 Answers2

0

Use preg_replace function.

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

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

francisco
  • 1,387
  • 2
  • 12
  • 23
Maksym K.
  • 11
  • 2
0

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?.

hakre
  • 193,403
  • 52
  • 435
  • 836