0

私は xml フィードからアイテム (この場合は製品) を取得する小さな PHP を作成しました。simplexml_load_file を使用して、フィードのさまざまな部分を変数に割り当てます。

それはうまく機能しており、結果をcsvファイルに出力しています。私が抱えている問題は、xml の項目の一部が html 記述であり、改行文字が含まれていることです。これらを新しい行と見なすため、これは csv を壊しています。

これを解決するために、csv の各行に行末文字列を追加し、説明からすべての \n\r を削除してから、eol 文字列を \n に置き換えて、csv を機能させる必要があります.

notepad ++で手動で実行できますが、phpで試してみるとうまくいかないようです。

以下は私が試したコードですが、これまでにこれを行ったことがないため、何かを台無しにした可能性があり、機能していないようです。結果の csv にはまだ元の改行があり、eol 文字列はまだそこにあります。

//Get The Short and Long Descriptions from the $_item in the XML
   $short_Description = $_item->short_description;
   $description = $_item->description;

//OK now we need to firstly remove all the \n\r \n \r from the descriptions and once that's done replace "FEOL," with \n. Easy!

// Lets strip the LF and CR from the descriptions   
    $new_Line = array('\n\r', '\n', '\r');
    $replace = '';

    $short_Description = str_replace($new_Line, $replace, $short_Description);
    $description = str_replace($new_Line, $replace, $description);

// OK now we have one big long line lets add the CR or maybe LF back at the end of each line where we've put the FEOL place holder
    $eol   = 'FEOL,';
    $replace = '\r';

    $short_Description = str_replace($eol, $replace, $short_Description);
    $description = str_replace($eol, $replace, $description);

彼らが私が行方不明であることが明らかな何かがあるのか​​ 、それとも私がすべて間違っているのか疑問に思っています。どんな助けでも大歓迎です。ナイジェル

4

2 に答える 2

1

ここでは、LF および CR 文字の代わりに「スラッシュ n」および「スラッシュ r」を置き換えています。

$new_Line = array('\n\r', '\n', '\r');

二重引用符を使用します。

$new_Line = array("\n\r", "\n", "\r");

あなたは違いを自分で見ることができます:

<?php
print '\n';

印刷します: \n

その間

<?php
print "\n";

改行文字を出力します

「\r\n」ではなく「\n\r」でよろしいですか?

于 2013-09-21T12:04:47.283 に答える
0

これは、PHP ドキュメント サイトの例です。

http://php.net/manual/en/function.str-replace.php

$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '';

$newstr = str_replace($order, $replace, $str);

ご覧のとおり、各配列項目を二重引用符で囲み、これまでとは違う方法で置換する必要があり\r\nます\n\r

于 2013-09-21T12:17:55.550 に答える