I have a PHP script that does the following:
- Uses file_get_contents() to get content of html file
- Echos a JSON object
The issue is that the value obtained from file_get_contents is multi line. It needs to be all on one line in order to be in correct JSON format.
For example
PHP File:
$some_json_value = file_get_contents("some_html_doc.html");
echo "{";
echo "\"foo\":\"$some_json_value\"";
echo "}";
The resulting html document looks like:
{
foo: "<p>Lorem ipsum dolor
sit amet, consectetur
adipiscing elit.</p>"
}
My goal is to get the resulting html document to look like this (value is one line, not three)
{
foo: "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"
}
How can this be done. I realize that the content will be one line if the original html doc is one line; however, I'm trying to avoid that solution.
Update
The question was answered correctly. Here is the complete, working code:
$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 "}";