22 Votes

PHP: Replace Byte Sequence in String

Question by Guest | Last update on 2022-05-01 | Created on 2014-03-11

I would like to prepare some text using a PHP script so that it can be output.

Beyond usual ASCII and ANSI characters, the origin file contains some bytes and byte sequences, that cannot be displayed reasonably.

Therefore, I would like to replace some of those byte sequences (there are only a few) with displayable characters and letters.

I know all of the bytes and characters I would like to use for the replacement, but however, up to now, I have only replaced normal strings and characters using str_replace in PHP.

Hence my question is: How is it possible to replace, for example, the code points E2 80 93 with the character "-"?

ReplyPositiveNegative
2Best Answer2 Votes

If you have the hexadecimal code of a character (for example E2), the hexadecimal writing for this character would be \xE2 in PHP.

To replace a byte sequence consisting of multiple hexadecimal codes, you can just write several HEX-characters in a row:

$s = str_replace("\xE2\x80\x93", '-', $s);

This example would replace the byte sequence E2 80 93 in the string $s with "-".
Last update on 2022-10-31 | Created on 2014-03-14

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.