简体   繁体   中英

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 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. 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>\\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"},

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?

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("\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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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