我试图摆脱不必要的文本在我的数据库内容。我的代码是这样的:
if(mysql_num_rows($result))
$items[] = array();
while($row = mysql_fetch_assoc($result)) {
$items[] = array('id' => $row['id'], 'cat' => $row['cat'], 'type' => $row['type'], 'name' => $row['name'], 'sub_title' => $row['sub_title'], 'display_date' => $row['display_date'], 'slug' => $row['slug'], 'ticket_url' => $row['ticket_url'], 'status' => $row['status'], 'content' => $row['content'], 'display_until' => $row['display_until'], 'photo' => $row['photo'], 'thumb' => $row['thumb']);
$removals = array(''n',''r',''t','<'/div>'r'n');
$spaces = "";
$parsedText = str_replace($removals, $spaces, $items);
}
echo json_encode(array('events'=>$items));
然后内容显示如下:
{"events":[[],{"id":"66","cat":"9","type":"2","name":"Oileán - Blasket群岛庆典","sub_title":"National Folk Theatre","display_date":"周二5月4日至周四5月6日晚上8:30 ","slug":"这部作品庆祝过去Blasket群岛的生活,探索岛民的生活方式和生存精神。Oileán抓住了这个岛屿社区的精髓,他们的传统和习俗,他们丰富的歌曲和故事,他们对生活的热爱以及他们彼此之间强烈的亲属关系。"、"ticket_url":","状态":"1"、"内容":"'r'n't由爱尔兰国家民间剧院的成员呈现,这部作品颂扬和探索了布拉斯基特岛的生活,同时也挑战了我们作为当代岛民的身份观念。& lt;/div> ' r ' n' r ' n ' t & lt;/div> ' r ' n'r'n'于2003年首播,Oileán纪念Blasket岛民离开大陆50周年。位于西克里海岸外的大毯子岛对许多来自爱尔兰和国外的人来说仍然具有几乎神秘的意义。岛民的生活方式和他们的生存精神在这部作品中得到了体现,它抓住了这个岛屿社区的本质,他们的传统和习俗,他们丰富的歌曲和故事,他们对生活的热爱以及他们彼此之间强烈的亲属关系。& lt;/div> ' r ' n' r ' n ' t & lt;/div> ' r ' n'r'n'tOileán通过舞蹈、哑剧、音乐和歌曲等媒介,以独特的暹罗舞风格呈现。
上面显示的是数据库中的第一项。我尝试替换所有的'r, 'n等在上述内容?我该怎么做呢?我所做的是正确的吗?
2件事
if(mysql_num_rows($result))
$items = array(); // not $items[], that would set the first item as an array
while($row = mysql_fetch_assoc($result)) {
$removals = array("'n","'r","'t","<'/div>'r'n");
$spaces = "";
$items[] = array(
'id' => $row['id'],
'cat' => $row['cat'],
'type' => $row['type'],
'name' => $row['name'],
'sub_title' => $row['sub_title'],
'display_date' => $row['display_date'],
'slug' => $row['slug'],
'ticket_url' => $row['ticket_url'],
'status' => $row['status'],
// replace the content here
// youll want to use preg_replace though otherwise youll end up with multiple </div>'s
'content' => str_replace( $removals, $spaces, $row['content'] ),
'display_until' => $row['display_until'],
'photo' => $row['photo'],
'thumb' => $row['thumb']
);
}
echo json_encode(array('events'=>$items));