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

▼魔方 西西 提交于 2019-12-06 14:46:06

问题


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.



来源:https://stackoverflow.com/questions/9972871/how-to-remove-ansi-code-13-from-string-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!