April 2, 2012

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

Question by ESCOBAR

I have tried a lot but nothing is working. I want to import a XML-file with PHP. In some strings the customer puts some ANSI-Code Carrier Returns (“
”). I have tried to remove them with:

str_replace('r', '', $xml->description);

I also tried it with "
", "rn", "&\#13;" in the search but nothing works. Do you have any idea how to remove these linebreaks?

Thanks!

Answer by Corbin

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

You just need to use “n” or maybe “rn”.

Answer by Starx

Actually, This runs just fine

$str = "
"; //just an example

echo str_replace("&\#13;", "hello", $str);

Demo

...

Please fill the form - I will response as fast as I can!