转义记录并添加到数组的奇怪问题

时间:2016-02-17 02:52:08

标签: php arrays json

我有一些PHP代码用于从数据库中提取记录,将它们放入数组,将该数组放入另一个数组,然后将该数组格式化为JSON。连接到数据库并提取信息非常有用,构建第二个数组并转换为JSON也是如此。但是当放入第一个阵列时,某些记录(175个中的12个)显然是空白的。我确定它与非转义和非标准字符有关,但我不能为我的生活弄清楚我需要做些什么来确保它正确地添加到数组中。我尝试添加addslasheshtmlentities以确保它不是标准的转义问题,但它仍然在发生。所有记录目前都编码为ASCII。

以下结果在添加到数组时会显示为空白:

A Gentlewoman’s Lament
By true record of the Doctor Bede
A Lover’s Lament
Complaint þat Crist maketh of his Passioun
How þe louer is sett to serve þe floure
Fifteen O’s of Christ
Balade made by Lydgate at þe Departyng of Thomas Chaucyer on Ambassade into France
On the Mutability of Man’s Nature due to the Seasons, the Elements, the Complexions, and the Planets
That now is hay þat sumtyme was grasse
Balade on a New Year’s Gift of an Eagle presented to King Henry VI in 1428
Everything draweþe to his semblable
Pélerinage de la vie humaine

这是我用来从SQL结果中提取信息的代码:

while ($row = $work_result->fetch_array())
{
    echo $row['Work'] . "<BR/>";
    $encoding = mb_detect_encoding(htmlentities($row['Work']));
    $work_array = array('id'=>$row['id'],'name'=>addslashes(htmlentities($row['Work'])),'encoding'=>$encoding, 'type'=>'work','color'=>'midnightblue');
    var_dump($work_array);
    echo "<BR/>";
    array_push($array, $work_array);
}

我完全失去了。我确定有一个我只是不使用的功能,但它是什么我不知道。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用urlencode()对字符串进行编码。

Native