我有非常大的数据库来清理。在旧系统上,用户被允许使用HTML(并从Word文件粘贴)。现在我在DB中的不同位置有大量的开放标签。
任何人都知道要执行此类清理的应用程序/脚本吗?
答案 0 :(得分:2)
您可以使用某些PHP(或其他后端语言)
来执行此操作这是一个用php编写的例子
$link = mysql_connect($host, $username, $password); //connecting to database
mysql_select_db($database_name,$link); // select a database
$q = mysql_query('SELECT id_row,cell_with_tags FROM tablename WHERE some_condition_if_it_needed');
if(mysql_num_rows($q) !== 0){ //check if result of our query not empty
while($res = mysql_fetch_assoc($q)){ // gets all rows-cells as associative array
$result[] = array('id_row'=>$res['id_row'],
'cell_with_tags'=>strip_tags($res['cell_with_tags']);
}
}
if(!empty($result)){
foreach($result as $k=>&$v){ // update our database. Write there values without tags
mysql_query('UPDATE tablename SET cell_with_tags = '.$result[$k]['cell_with_tags'].' WHERE id='.$result[$k]['id_row']);
}
}
如果你想删除并非所有的html标签,你也可以修改strip_tags函数的调用。 (http://ru.php.net/manual/en/function.strip-tags.php)
如果无法执行php脚本,则只能使用mysql执行此操作。我是从this
得到的DROP FUNCTION IF EXISTS strip_tags ||
CREATE FUNCTION strip_tags( x longtext) RETURNS longtext
LANGUAGE SQL NOT DETERMINISTIC READS SQL DATA
BEGIN
DECLARE sstart INT UNSIGNED;
DECLARE ends INT UNSIGNED;
SET sstart = LOCATE('<', x, 1);
REPEAT
SET ends = LOCATE('>', x, sstart);
SET x = CONCAT(SUBSTRING( x, 1 ,sstart -1) ,SUBSTRING(x, ends +1 )) ;
SET sstart = LOCATE('<', x, 1);
UNTIL sstart < 1 END REPEAT;
return x;
END;
||
delimiter ;
mysql> SELECT strip_tags('<a href="HelloWorld.html"><B>Hi, mate!</B></a>') as strip_tags;
答案 1 :(得分:0)
如果你有&lt;和&gt;字符作为文本的一部分,而不是HTML的一部分。即使您的字符串在字符串末尾包含损坏的HTML标记。那么你应该使用这个版本的函数:
SET GLOBAL log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS fnStripTags;
DELIMITER |
CREATE FUNCTION fnStripTags( Dirty varchar(4096) )
RETURNS varchar(4096)
DETERMINISTIC
BEGIN
DECLARE iStart, iEnd, iLength, DirtyLength, DirtyTail int;
DECLARE iNextLessThenAngleBracket, iLengthToNextLessThenAngleBracket int;
WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
BEGIN
SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
SET iNextLessThenAngleBracket = Locate( '<', Dirty, Locate('<', Dirty ) + 1);
SET iLength = ( iEnd - iStart) + 1;
SET iLengthToNextLessThenAngleBracket = ( iNextLessThenAngleBracket - iStart) + 1;
IF iLength < iLengthToNextLessThenAngleBracket THEN
BEGIN
IF iLength > 0 THEN
BEGIN
SET Dirty = Insert( Dirty, iStart, iLength, '');
END;
END IF;
END;
ELSE
BEGIN
IF iNextLessThenAngleBracket != 0 THEN
BEGIN
SET Dirty = Insert( Dirty, iStart, 1, '*/*');
END;
ELSE
BEGIN
SET Dirty = Insert( Dirty, iStart, iLength, '');
END;
END IF;
END;
END IF;
END;
END WHILE;
IF Locate( '<', Dirty ) > 0 THEN
BEGIN
SET DirtyLength = CHAR_LENGTH(Dirty);
SET DirtyTail = DirtyLength - Locate( '<', Dirty ) + 1;
SET Dirty = Insert( Dirty, Locate( '<', Dirty ), DirtyTail, '');
END;
END IF;
BEGIN
SET Dirty = REPLACE(Dirty, '*/*', '<');
END;
RETURN Dirty;
END;
|
DELIMITER ;