How to remove ANSI-Code ("
") from string in PHP

✔ Recommended Answer

Since your XML processor is already handling de-entitying the entities, you'll be left over with plain ASCII or \r or \r. PHP does not handle \r or inside of single quotes. It only translates them to their respective characters (codes 10 and 13), when the \r and are inside of double quotes.

You just need to use "" or maybe "\r".


Answered By: Corbin

Method #2

You can remove ANSI codes or other non-printable characters, including 
, from a string in PHP using the preg_replace() function with a regular expression.

Here's an example code snippet to remove 
 from a string:

$string = "This is a string with 
 ANSI codes."; $cleanString = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $string); echo $cleanString; // Output: This is a string with ANSI codes.

In this example, we have a string containing the 
 ANSI code. We then use the preg_replace() function with a regular expression to remove any non-printable characters from the string.

The regular expression /[\x00-\x1F\x7F-\xFF]/ matches any character that has an ASCII value between 0-31 or 127-255, which includes all non-printable characters.

The preg_replace() function replaces any character that matches the regular expression with an empty string, effectively removing it from the original string. The resulting string is assigned to the variable $cleanString, which is then printed to the screen using the echo statement.

You can adjust the regular expression to match and remove other non-printable characters as needed.


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755