换行'吓坏了我?

时间:2011-11-10 16:47:39

标签: php string oracle fusioncharts

我有一个带有融合图表的应用程序,我必须为他生成XML,以便为用户呈现一些图形。但是这个XML字符串必须是内联的,没有断行和stuff东西。但是我检索图形数据的查询给我带来了这些断行的结构!

我在字符串上尝试了这种格式:

$txt = trim($txt);
$txt = str_replace("\r\n", " ", $txt);
$txt = preg_replace('~[\n|\r|\n\r|\r\n]{2,}~', " ", $txt);

这是带有此问题的数据示例:

- Eixo Inferior Entrada Com Camisa

- Montar As Calhas Aparafusadas Conforme Posição Anterior.
- Montar O Rodete Z15 - Ext. 1200.
- Manter A Identificação Na Peça.

数据库:Oracle - 列数据类型:Varchar2

2 个答案:

答案 0 :(得分:2)

尝试:

$txt = preg_replace('~(*BSR_ANYCRLF)\R~', " ", $txt);
$txt = trim($txt);

用空格替换所有中断,修剪结果。

如果你想支持unicode,这是utf-8变种:

$txt = preg_replace('~\R~u', " ", $txt);
$txt = trim($txt);

有关详细信息,请参阅How to replace different newline styles in PHP the smartest way?

答案 1 :(得分:0)

如果你的图形数据包含Linux风格的换行符(“\ n”),你尝试的格式不起作用,因为它只匹配“\ r \ n”。

此代码应清除Windows和Linux风格换行符的数据;

$txt = trim($txt)
$txt = str_replace("\r", " ", $txt);
$text = str_replace("\n", " ", $text);