简体   繁体   English

如何替换mysql数据库内容数组中的文本

[英]how to replace text in a mysql database content array

im trying to get rid of unneccesary text in my database content.My code looks like this: 我正在尝试摆脱数据库内容中不必要的文本。我的代码如下所示:

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));

And the content then displays like this: 然后内容显示如下:

{"events":[[],{"id":"66","cat":"9","type":"2","name":"Oileán - A Celebration of the Blasket Islands","sub_title":"National Folk Theatre","display_date":"Tues 4th - Thurs 6th May at 8.30pm","slug":"This production celebrates life on the Blasket Islands in times past, exploring the way of life of the islanders and their spirit of survival. Oileán captures the essence of this island community, their traditions and customs, their wealth of song and story, their love of life and their strong kinship with one another. ","ticket_url":"","status":"1","content":" \\r\\n\\tPresented by the members of the National Folk Theatre of Ireland</strong>, this production celebrates and explores Blasket Island living while also challenging our own notions of identity as contemporary islanders. </div>\\r\\n \\r\\n\\t </div>\\r\\n \\r\\n\\tPremiered in 2003, Oileán</strong></em> marked the 50th</sup> anniversary of the departure of the Blasket Islanders to the mainland. The Great Blasket Island, located off the coast {“ events”:[[],{“ id”:“ 66”,“ cat”:“ 9”,“ type”:“ 2”,“ name”:“Oileán-Blasket Islands庆祝活动”,“ sub_title“:”国家民俗剧院“,” display_date“:” 5月4日至5月6日,星期四,8.30pm“,” slug“:”此作品庆祝了过去时代在Blasket群岛上的生活,探索了岛民的生活方式“,” ticket_url“:”“,”状态“和”生存精神“,奥利昂(O'eánán)捕捉了这个岛屿社区的精髓,他们的传统和习俗,丰富的歌曲和故事,对生活的热爱以及彼此之间的亲密血缘。 “:” 1“,”内容“:” \\ r \\ n \\ t由爱尔兰国家民俗剧院成员主持</ strong>,该作品既庆祝并探索了布拉斯科特岛的生活,同时也挑战了我们自己的当代身份观念</ div> \\ r \\ n \\ r \\ n \\ t </ div> \\ r \\ n \\ r \\ n \\ tOileán</ strong> </ em>成立于2003年,庆祝成立50周年布拉斯凯特岛民离开大陆的旅程大布拉斯凯特岛位于沿海 of West Kerry still retains an almost mystical significance for many, both from Ireland and abroad. 对于来自爱尔兰和国外的许多人来说,西凯里岛仍然具有几乎神秘的意义。 The way of life of the islanders and their spirit of survival is framed in this production, which captures the essence of this island community, their traditions and customs, their wealth of song and story, their love of life and their strong kinship with one another. 这次拍摄展示了岛民的生活方式和生存精神,充分体现了该岛社区的精髓,他们的传统和习俗,丰富的歌曲和故事,对生活的热爱以及彼此之间的亲密血缘。 </div>\\r\\n \\r\\n\\t </div>\\r\\n \\r\\n\\tOileán</i></b> is delivered in the unique Siamsa style through the medium of dance, mime, music and song.</div>\\r\\n \\r\\n\\t </div>\\r\\n \\r\\n\\t \\r\\n\\t\\t </div>\\r\\n\\t \\r\\n\\t\\tPlease note that due to the popularity of performances by the National Folk Theatre</strong>, some productions may be sold out well in advance and tickets may not be available on-line. </ div> \\ r \\ n \\ r \\ n \\ t </ div> \\ r \\ n \\ r \\ n \\tOileán</ i> </ b>以独特的暹罗风格通过舞蹈,哑剧形式传播,音乐和歌曲。</ div> \\ r \\ n \\ r \\ n \\ t </ div> \\ r \\ n \\ r \\ n \\ t \\ r \\ n \\ t \\ t </ div> \\ r \\ n \\ t \\ r \\ n \\ t \\ t请注意,由于国家民俗剧院的表演很受欢迎</ strong>,某些作品可能会提前售罄,并且可能无法在线购买门票。 However, we often have returns and tickets may be available nearer to the day of a performance</strong>. 但是,我们经常会有退货,门票可能会在演出当天附近提供</ strong>。 Please contact us directly by phone on: +353 (0)66 7123055.</em></div>\\r\\n\\t \\r\\n\\t\\t </div>\\r\\n\\t \\r\\n\\t\\t </div>\\r\\n</div>\\r\\n","display_until":"20100504","photo":"1269869378-oilean_side.jpg","thumb":"1269869378-oilean_thumb.jpg"}, 请直接通过电话与我们联系:+353(0)66 7123055。</ em> </ div> \\ r \\ n \\ t \\ r \\ n \\ t \\ t </ div> \\ r \\ n \\ t \\ r \\ n \\ t \\ t </ div> \\ r \\ n </ div> \\ r \\ n“,” display_until“:” 20100504“,”照片“:” 1269869378-oilean_side.jpg“,”拇指“:” 1269869378 -oilean_thumb.jpg“},

The above display is the first item in the DB. 上面的显示是数据库中的第一项。 Im trying the replace all the \\r , \\n , etc in the above content?How can i go about this?Is what i have allready on the right track? 我正在尝试替换上述内容中的所有\\ r,\\ n等?我该如何解决?我已经准备好了吗?

2 things 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM