php - 如何获取json格式的html?
问题描述
我有一个 PHP API,可以将数据返回到我的 android 应用程序,数据库中的一些数据是 Html 格式的。有时 API 返回所有内容,但返回一些Html 数据时,API 没有返回任何内容,这就是 API。
//Importing Database Script
require_once('dbConnect.php');
//Creating sql query
$sql="SELECT * FROM `blogs`";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"title"=>$row['title'],
"details"=>$row['details'],
"image"=>$row['photo'],
"source"=>$row['source'],
"views"=>$row['views'],
"date"=>$row['created_at']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
注意标题和描述是 Html 标题返回但细节不是
这是无效数据的示例
<div align="justify">The recording starts with the patter of a summer squall. Later, a
drifting tone like that of a not-quite-tuned-in radio station
rises and for a while drowns out
the patter. These are the sounds encountered by NASA’s Cassini
spacecraft as it dove
the gap between Saturn and its
innermost ring on April 26, the first of 22 such encounters before it
will plunge into
atmosphere in September. What
Cassini did not detect were many of the collisions of dust particles
hitting the spacecraft
it passed through the plane of
the ringsen the charged particles oscillate in unison.<br><br></div><h3 align="justify">How its Works ?</h3>
<p align="justify">
MIAMI — For decades, South
Florida schoolchildren and adults fascinated by far-off galaxies,
earthly ecosystems, the proper
ties of light and sound and
other wonders of science had only a quaint, antiquated museum here in
which to explore their
interests. Now, with the
long-delayed opening of a vast new science museum downtown set for
Monday, visitors will be able
to stand underneath a suspended,
500,000-gallon aquarium tank and gaze at hammerhead and tiger sharks,
mahi mahi, devil
rays and other creatures through
a 60,000-pound oculus. <br></p><p align="justify">Lens that will give the impression of seeing the fish from the bottom of
a huge cocktail glass. And that’s just one of many
attractions and exhibits.
Officials at the $305 million Phillip and Patricia Frost Museum of
Science promise that it will be a
vivid expression of modern
scientific inquiry and exposition. Its opening follows a series of
setbacks and lawsuits and a
scramble to finish the
250,000-square-foot structure. At one point, the project ran
precariously short of money. The museum
high-profile opening is
especially significant in a state s <br></p><p align="justify"><br></p><h3 align="justify">Top 5 reason to choose us</h3>
<p align="justify">
Mauna Loa, the biggest volcano
on Earth — and one of the most active — covers half the Island of
Hawaii. Just 35 miles to the
northeast, Mauna Kea, known to
native Hawaiians as Mauna a Wakea, rises nearly 14,000 feet above sea
level. To them it repre
sents a spiritual connection
between our planet and the heavens above. These volcanoes, which have
beguiled millions of
tourists visiting the Hawaiian
islands, have also plagued scientists with a long-running mystery: If
they are so close together,
how did they develop in two
parallel tracks along the Hawaiian-Emperor chain formed over the same
hot spot in the Pacific
Ocean — and why are their
chemical compositions so different? "We knew this was related to
something much deeper,
but we couldn’t see what,” said
Tim Jones.
</p>
解决方案
确保在将任何 HTML 数据放入 JSON 数组之前对其进行正确编码,否则可能会破坏它。利用htmlentities($html)
例如。"source"=>htmlentities($row['source'])
推荐阅读
- python-3.x - 如何改进椭圆曲线分解的代码?
- angular - Angular 6:如何在 Angular 材料表上实现分页和排序
- laravel-5 - 如何从两个模型的关系中调用数据
- python-3.x - 这个错误的原因是什么:“PermissionError: [WinError 5] Access is denied”
- ajax - 这个 ajax 调用的正确相对路径是什么?
- javascript - 即使在服务器和客户端上设置标头后,错误“请求的资源上不存在'Access-Control-Allow-Origin'标头”
- bioinformatics - x86_64-pc-mingw64 编译 hmmer 软件时出错
- javascript - 在其他地方初始化 'mymodule' 后调用 require('mymodule') 是否正确?
- php - 使用 AJAX 和 PHP 从联系表单发送邮件后,如何显示弹出警报消息?
- c# - 为什么在 AVAudioEngine 中点击麦克风时缓冲区中的 BytesPerFrame 异常巨大