1

RSSフィードからデータを取得し、モバイルアプリ用に別の形式で提供できるように保存するPHPアプリを作成しています。

文字列からデータを取得する重要なビットを除いて、すべてが正常に機能しています。データには明らかに新しい行がありますが、私はそれを爆発させることはできません!これが、各行を配列に格納しようとするこれまでの私の試みです。私は自分の知識とグーグルの結果を使い果たしました!

    // Prepare the data
    $possibleLineEnds = array("\r\n", "\n\r", "\r");
    $preparedData = trim($row['description']);

    // Loop through and replace the data
    foreach ($possibleLineEnds as $lineEnd) {

      $preparedData = str_replace($lineEnd, "\n", $preparedData);
    }

    // Explode the data into new rows
    $locationData = explode("\n", $preparedData);

    print_r($locationData);

どんなアイデアでも、何でもこれで歓迎されます!


私は10の評価を持っていないので、これを回答済みとしてマークすることはできません。

私はそれを動かしています!私はそれができるほどきれいではないことを知っています、私はpreg関数のパターンを理解していません!

動作したコードは次のとおりです。

    // Prepare the data
    $possibleLineEnds = array("\r\n", "\n\r", "\r", "<br>", "<br/>", "&lt;br/&gt;");
    $preparedData = trim(htmlspecialchars_decode($row['description']));

    // Replace the possible line ends
    $preparedData = str_replace($possibleLineEnds, "\n", $preparedData);

    // Explode the data into new rows
    $locationData = explode("\n", $preparedData);

    print_r($locationData);

皆様のご意見ありがとうございました!

4

4 に答える 4

1

改行できない場合。一意の文字列で分割。

// Prepare the data
$possibleLineEnds = array("\r\n", "\n\r", "\r", "\n");
$preparedData = trim($row['description']);

// Loop through and replace the data
foreach ($possibleLineEnds as $lineEnd) {

  $preparedData = str_replace($lineEnd, ":::", $preparedData);
}

// Explode the data into new rows
$locationData = explode(":::", $preparedData);

print_r($locationData);
于 2013-02-01T11:27:50.333 に答える
1

I would just use preg_split() to match any combination of \n and \r characters, rather than messing with the string using str_replace() just so we can explode() it.

Your entire code is reduced to a single line:

$output = preg_split('/(\n|\r)+/', $input);

The only difference between this and your original solution is that if the input contains blank lines, they won't appear in the exploded output. But I think that's probably a good thing in your case.

于 2013-02-01T11:18:40.937 に答える
1

私はそれが働いている!私はそれが可能な限りきちんとしていないことを知っています.preg関数のパターンを理解していません!

機能したコードは次のとおりです。

// Prepare the data
$possibleLineEnds = array("\r\n", "\n\r", "\r", "<br>", "<br/>", "&lt;br/&gt;");
$preparedData = trim(htmlspecialchars_decode($row['description']));

// Replace the possible line ends
$preparedData = str_replace($possibleLineEnds, "\n", $preparedData);

// Explode the data into new rows
$locationData = explode("\n", $preparedData);

print_r($locationData);

皆様のご意見をお寄せいただきありがとうございます。

于 2013-02-03T09:44:38.063 に答える
0

私が通常使用するものは次のとおりです。

$preparedData = str_replace("\r", "", $preparedData);
$locationData = explode("\n", $preparedData);

これは私にとって常にうまくいきました。

于 2013-02-01T11:13:37.773 に答える