最近,研究了一下discuzX3.1批量自动添加tags标签,发现只要同时改几个表就可以进行自动批量的添加tags标签,这个标签库是discuz官方的。代码如下:
Code:
01.<?php
02.
03.#设置编码
04.header("Content-type:text/html;charset=gbk");
05.
06.#连接数据库
07.$conn=mysql_connect('localhost','root','123456')ordie('Couldnotconnect:'.mysql_error());
08.mysql_select_db("db",$conn);
09.mysql_query("SETcharacter_set_results='gbk',character_set_client='gbk',character_set_connection='utf8',character_set_database='utf8',character_set_server='utf8'",$conn);
10.
11.#开始执行
12.echosettags();
13.
14.#得到tags为空的主题
15.functionsettags(){
16.$key="";
17.$i=1;
18.$reslut=mysql_query("selecttid,subject,messagefrompr_forum_postwheretags=''");
19.while($row=mysql_fetch_array($reslut)){
20.$message=$row['message'];
21.$message=preg_replace("/\[.+?\]/U",'',$message);
22.getkey($row['tid'],$message);
23.#echo$row['tid']."||||".dhtmlspecialchars($row['message']).'<br><br>';
24.}
25.}
26.
27.#得到tags标签,并且更新到pr_forum_post表
28.functiongetkey($tid,$contents){
29.$rows=strip_tags($contents);
30.$arr=array('','',"\s","\r\n","\n","\r","\t",">","“","”","<br/>");
31.$qc_rows=str_replace($arr,'',$rows);
32.if(strlen($qc_rows)>2400000000){
33.$qc_rows=substr($qc_rows,'0','24000000000');
34.}
35.#echo$qc_rows;
36.$qc_rows=rawurlencode(strip_tags(preg_replace("/\[.+?\]/U",'',$qc_rows)));
37.$data=@implode('',file("http://keyword.discuz.com/related_kw.html?title=$qc_rows&ics=gbk&ocs=gbk"));
38.preg_match_all("/<kw>(.*)A\[(.*)\]\](.*)><\/kw>/",$data,$out,PREG_SET_ORDER);
39.$key="";
40.#echo$tid;
41.#print_r($out).'<br><br>';
42.for($i=0;$i<count($out);$i++){
43.$key=$key.gettagid($tid,$out[$i][2]);
44.#$key=$key.$out[$i][2];
45.}
46.mysql_query("updatepr_forum_postsettags='".$key."'wheretid=".$tid."");
47.#echo$tid.'--'.$key.'<br>';
48.}
49.
50.#同步更新pr_common_tag、pr_common_tagitem表
51.functiongettagid($tid,$word){
52.$key='';
53.$tagid='';
54.$result=mysql_query("selecttagid,tagnamefrompr_common_tagwheretagname='".$word."'");
55.if($row=mysql_fetch_array($result)){
56.$tagid=$row['tagid'];
57.}else{
58.mysql_query("insertintopr_common_tag(tagname,status)values('".$word."',0)");
59.$tagid=mysql_insert_id();
60.}
61.mysql_query("deletefrompr_common_tagitemwhereitemidin(".$tid.")");
62.mysql_query("insertintopr_common_tagitemvalues('".$tagid."','".$tid."','tid')");
63.$key=$tagid.",".$word."";
64.return$key;
65.}
66.
67.#字符过滤
68.functiondhtmlspecialchars($string){
69.if(is_array($string)){
70.foreach($stringas$key=>$val){
71.$string[$key]=dhtmlspecialchars($val);
72.}
73.}else{
74.$string=str_replace(array('&','"','<','>'),
75.array('&','"','<','>'),$string);
76.if(strpos($string,'&#')!==false){
77.$string=preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/',
78.'&\\1',$string);
79.}
80.}
81.return$string;
82.}
83.
84.
85.?>
注:执行上面代码可能要多执行几次,如果数据量比较大的时候,因为会执行超时。