记录一次网站改版“洗数据”的过程

背景:公司接到清华的一个开发清华五道口官网的项目。需要对以前其官网上的数据进行迁移。


要求:一般的比如新闻内容这个字段会有非常多的数据,而原来的网站这个字段的数据中存储了非常多的标签,如:<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。如果是其他情况其他框架需要适当修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值