次のことを行うPHPスクリプトがあります。
- file_get_contents() を使用して、html ファイルのコンテンツを取得します
- JSON オブジェクトをエコーする
問題は、file_get_contents から取得した値が複数行になることです。正しい JSON 形式にするためには、すべてを 1 行にする必要があります。
例えば
PHP ファイル:
$some_json_value = file_get_contents("some_html_doc.html");
echo "{";
echo "\"foo\":\"$some_json_value\"";
echo "}";
結果の html ドキュメントは次のようになります。
{
foo: "<p>Lorem ipsum dolor
sit amet, consectetur
adipiscing elit.</p>"
}
私の目標は、結果の html ドキュメントを次のようにすることです (値は 3 行ではなく 1 行です)。
{
foo: "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"
}
これはどのように行うことができますか。元のhtmlドキュメントが1行の場合、コンテンツは1行になることを認識しています。ただし、その解決策を回避しようとしています。
アップデート
質問は正しく答えられました。完全な動作コードは次のとおりです。
$some_json_value = file_get_contents("some_html_doc.html");
$some_json_value = json_encode($some_json_value); // this line is the solution
echo "{";
echo "\"foo\":\"$some_json_value\"";
echo "}";