背景:公司接到清华的一个开发清华五道口官网的项目。需要对以前其官网上的数据进行迁移。
要求:一般的比如新闻内容这个字段会有非常多的数据,而原来的网站这个字段的数据中存储了非常多的标签,如:<br/>、内联样式等等。而我们一般只能存在<p>、,<span>标签。
解决方法:将其他标签替换、将不要的东西去掉。
function ChangeContent($str){
// $str = str_replace(array('\r','\n','\r\n','\n\r'), '', $str);
// $str = preg_replace("/<span[^>]><span[^>]>/","<p>",$str);
// $str = str_replace("</sup>","<p>",$str);
// $str = str_replace("<div><span>","<p>",$str);
// $str = str_replace("</div></span>","</p>",$str);
$str = str_replace(" ","",$str);
$str = str_replace('<span style="color:#666666;">', '', $str);
$str = str_replace("<br />\r\n<br />", "</p><p>", $str);
$str = str_replace("<br />", "", $str);
$str = preg_replace('/<div[^>]*>/', '', $str);
$str = str_replace('</div>', '', $str);
$str = preg_replace('/style="[^"]*"/', '', $str);
$str = preg_replace('/<\/span><span[^>]*>/', '', $str);
$str = preg_replace('/<span[^>]*>/', '<p>', $str);
$str = str_replace('</span>', '</p>', $str);
$str = str_replace('<p><p><p>', '<p>', $str);
$str = str_replace('</p></p></p>', '</p>', $str);
$str = str_replace('<p><p>', '<p>', $str);
$str = str_replace('</p></p>', '</p>', $str);
$str = str_replace("<p>\r\n<p>", "<p>", $str);
$str = str_replace("<p>\r\n", "<p>", $str);
$str = str_replace("</p>\r\n<p>", "</p><p>", $str);
$str = str_replace("</p>\r\n</p>", '</p>', $str);
$str = str_replace("\r\n", '', $str);
$str = preg_replace('/(<p>)$/', '', $str);
// $str = preg_replace("/<span[^>]*>/","<p>",$str);
// $str = preg_replace("/<p><span[^>]*><span[^>]*>/","<p>",$str);
// $str = preg_replace("/<span[^>]*>/","<p>",$str);
// $str = str_replace("</span></span></p>","</p>",$str);
// $str = str_replace("</span>","</p>",$str);
// $str = str_replace("<br />", "", $str);
// $str = str_replace("<br>", "</p><p>", $str);
// $str = str_replace("<br/>", "</p><p>", $str);
// $str = preg_replace('/<p[^>]*>/','<p>',$str);
// $str = str_replace('<div align="center">','<p align="center">',$str);
// $str = strip_tags($str, '<p><img><strong>');
// $str = preg_replace('/<strong[^>]*>/','<strong>',$str);
// $str = str_replace('src="','src="/userfiles/old',$str);
// $str = str_replace('src="/userfiles/oldhttp://info.ruc.edu.cn/','src="/userfiles/old/',$str);
// $str = str_replace('src="/userfiles/oldhttp://info.ruc.edu.cn:8090/','src="/userfiles/old/',$str);
// $str = str_replace('src="/userfiles/oldhttp://202.112.113.226/','src="/userfiles/old/',$str);
//$str = str_replace(CHR(10),"<br/>",$str);
//$str = '<p>'.$str.'</p>';
return $str;
}
// 新闻
global $mypdo;
$sql = "select voice_id,voice_content from eecenter_voice";
// $rs = mysql_query($sql);
$rs = $mypdo->sqlQuery($sql);
foreach ($rs as $r) {
$content = ChangeContent($r['voice_content']);
// var_dump($content);
$data = array (
'voice_content' => array('string', $content)
);
$where = array(
"voice_id" => array('number', $r['voice_id'])
);
$mypdo->sqlupdate($mypdo->prefix.'voice', $data, $where);
}
这里是数据已经通过excel从原系统数据库导出,用通过excel导入到新系统了。如果没有可以直接从原数据库读相应字段,经过替换后在insert into到当前数据库对应的字段中。当然还有当前用到的是wiipuPHP框架和pdo。如果是其他情况其他框架需要适当修改。