dongzhanlu0658 2011-11-08 04:54
浏览 98

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

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('
','','\t','<\/div>
');
    $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":" \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> \t </div> \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 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> \t </div> \tOileán</i></b> is delivered in the unique Siamsa style through the medium of dance, mime, music and song.</div> \t </div> \t \t\t </div> \t \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. However, we often have returns and tickets may be available nearer to the day of a performance</strong>. Please contact us directly by phone on: +353 (0)66 7123055.</em></div> \t \t\t </div> \t \t\t </div> </div> ","display_until":"20100504","photo":"1269869378-oilean_side.jpg","thumb":"1269869378-oilean_thumb.jpg"},

The above display is the first item in the DB. Im trying the replace all the , , etc in the above content?How can i go about this?Is what i have allready on the right track?

  • 写回答

1条回答 默认 最新

  • dongxian5735 2011-11-08 07:09
    关注

    2 things

    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("
    ","","\t","<\/div>
    ");
       $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));
    
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路