How to remove ANSI-Code (“ ”) from string in P

2020-07-28 22:41发布

问题:

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 "&\#13;", "\r\n", "\&\\#13\;" in the search but nothing works. Do you have any idea how to remove these linebreaks?

Thanks!

回答1:

Since your XML processor is already handling de-entitying the entities, you'll be left over with plain ASCII \n or \r or \r\n. 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 "\r\n".



回答2:

Should just be a simple case of:

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

Notice I haven't escaped the # with a \.



回答3:

Actually, This runs just fine

$str = "&\#13;"; //just an example

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

Demo



回答4:

This worked for me:

str_replace("\x13", '', $str);

I'm using the hexcode for that char.



标签: php string ansi