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".
Source: stackoverflow.com
Answered By: Corbin
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:
php$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.
Comments
Post a Comment