搜索和替换文件或目录的一个好类–很实用_php基础_脚本之家

数据库中,存放文章的表中有“Tag”字段,用来存放标签。标签之间用“,”分隔。比如“PHP,VB,随笔”。
下面的实现代码,将标签从数据库中搜出来,并格式化处理,使其以出现的次数为依据显示出不同大小的文字连接。
其中的细节,不做解释了! 观念陈、方法笨、效率低的标签云的实现代码如下:
复制代码 代码如下: /** * WNiaoBlog Tag
Template ShowTag * * @package WNiaoBlog * * @subpackage Tag */
//Connect the database //include(‘../include/config.php’); /** *
CountTag() – Statistics labels appear the number,and the data to be
stored in the two array * * GetTag() – Access the Tag’s Labels from
the database */ function CountTag{ $TagString = $String; //echo
$TagString.” “; $Tags = explode; $n = 1; $i = 0; $Continue = TRUE;
//echo $Tags[1].” “; //in case no-label’s article while($Tags[$n] OR
$Tags[++$n] OR $Tags[++$n] ){ $EachTag = $Tags[$n++]; //echo
$EachTag.” “; $Continue = TRUE; for{ if { if( $EachTagStr[$i][0] ==
$EachTag ){ $EachTagStr[$i][1]++; $Continue = FALSE; } else { if(
$EachTagStr[$i+1][0] ) $Continue = TRUE; else {
$EachTagStr[$i+1][0] = $EachTag; $EachTagStr[$i+1][1] = 1;
$Continue = FALSE; } } } else { //initialize the array
$EachTagStr[][] $EachTagStr[$i][0] = $EachTag;
$EachTagStr[$i][1] = 1; $Continue = FALSE; } } } return $EachTagStr;
} function ShowTag{ $i = 0; while{ $EachTag = $Row[$i][0];
$EachCount = $Row[$i][1]; $Size = SetSize; echo ” < a
style=’color:BLUE ; font-size:”.$Size.” ‘
onMouseOver=this.style.color=’#900000′
onMouseOut=this.style.color=’BLUE’
href='”.$ablink.”tag?tag=”.$EachTag.”‘ target=’_self’ >
“.$EachTag.””.” “; $i++; } } function GetTag(){ $QuerySet =
mysql_query(“select * from article”); while($Row =
mysql_fetch_array{ $Tag = $Row[‘tag’]; $TagString =
$TagString.”,”.$Tag; } return $TagString; } function SetSize{ $Size +=
10; if $Size = 30; return $Size; } //Go echo ” “; echo “标签云”; $String
= GetTag(); $Row = CountTag; ShowTag; echo ” “; ?> OK,DONE!

用户输入文本和输入的文本在过去的一个标签云
。标签云是一个用户生成的标签的可视化描述,或只是一个网站的文字内容,通常用来描述网站的内容。

这是个非常有用的程序,可以对文本文件进行特定的搜索,并以特定的文字替换指定的文字,举个例子说如果我这篇文章里有一个字全部打错了,有几十处,要一一找出来修改是件很麻烦的事,用下面这个就可以轻松搞定。–teaman.oso.com.cn
类文件 search_replace.inc find= $find; $this->replace= $replace;
$this->files= $files; $this->directories= $directories;
$this->include_subdir= $include_subdir; $this->ignore_lines=
$ignore_lines; $this->occurences= 0; $this->search_function =
‘search’; $this->last_error= ”; }
/***************************************
** Accessor for retrieving occurences.
***************************************/
function get_num_occurences(){ return $this->occurences; }
//获取最后的错误 function get_last_error(){ return
$this->last_error; } //设置FIND变量 function set_find{
$this->find = $find; } //设置replace变量 function set_replace{
$this->replace = $replace; } //设置FILE变量 function set_files{
$this->files = $files; } //设置目录变量 function set_directories{
$this->directories = $directories; } //设置目录变量
set_include_subdir function set_include_subdir{
$this->include_subdir = $include_subdir; } //设置ignore_lines变量
function set_ignore_lines{ $this->ignore_lines = $ignore_lines; }
//确定是哪一种搜索方式 function set_search_function{ switch{ case
‘normal’: $this->search_function = ‘search’; return TRUE; break;
case ‘quick’ : $this->search_function = ‘quick_search’; return
TRUE; break; case ‘preg’: $this->search_function = ‘preg_search’;
return TRUE; break; case ‘ereg’: $this->search_function =
‘ereg_search’; return TRUE; break; default: $this->last_error=
‘Invalid search function specified’; return FALSE; break; } }
//以下为搜索和替换程序的主文件 function search{ $occurences = 0;
$file_array = file; for($i=0; $i 0){ for($j=0; $jignore_lines[$j]))
== $this->ignore_lines[$j]) $continue_flag = 1; } } if continue;
$occurences += count(explode($this->find, $file_array[$i])) – 1;
$file_array[$i] = str_replace($this->find, $this->replace,
$file_array[$i]); } if $return = array($occurences, implode; else
$return = FALSE; return $return; }
//使用quick搜索方法时,没有igonre_lines功能 function quick_search{
clearstatcache(); $file= fread($fp = fopen, filesize; fclose;
$occurences = count(explode – 1; $file= str_replace($this->find,
$this->replace, $file); if $return = array; else $return = FALSE;
return $return; } //preg搜索方法不支持ignore_lines function
preg_search{ clearstatcache(); $file= fread($fp = fopen, filesize;
fclose; $occurences = count($matches = preg_split – 1; $file=
preg_replace($this->find, $this->replace, $file); if $return =
array; else $return = FALSE; return $return; }
//ereg搜索方法也不支持ignore_lines function ereg_search{
clearstatcache(); $file = fread($fp = fopen, filesize; fclose;
$occurences = count($matches = split -1; $file=
ereg_replace($this->find, $this->replace, $file); if $return =
array; else $return = FALSE; return $return; } //写新文件 function
writeout{ if($fp = @fopen{ fwrite; fclose; }else{ $this->last_error
= ‘Could not open file: ‘.$filename; } }
//由do_search调用,排出所有要搜索的文件 function do_files{
if(!is_array $this->files = explode; for($i=0; $ifiles[$i] == ‘.’
OR $this->files[$i] == ‘..’) continue; if(is_dir == TRUE)
continue; $newfile = $this->$ser_func; if == TRUE){
$this->writeout($this->files[$i], $newfile[1]);
$this->occurences += $newfile[0]; } } }
//由do_search()调用,排出所有要搜索的目录 function do_directories{
if(!is_array $this->directories = explode(‘,’,
$this->directories); for($i=0; $idirectories[$i]); while{ if($file
== ‘.’ OR $file == ‘..’) continue;
if(is_dir($this->directories[$i].$file) == TRUE){
if($this->include_subdir == 1){ $this->directories[] =
$this->directories[$i].$file.’/’; continue; }else{ continue; } }
$newfile = $this->$ser_func($this->directories[$i].$file); if
== TRUE){ $this->writeout($this->directories[$i].$file,
$newfile[1]); $this->occurences += $newfile[0]; } } } }
//调用这个do_search()就可以开始对文件或目录进行搜索 function
do_search(){ if{ if((is_array AND count OR $this->files != ”)
$this->do_files($this->search_function);
if($this->directories !=
”)$this->do_directories($this->search_function); } } } // End
of class ?> //下面是调用该类的例子说明,请存为example.php
set_search_function;//设置搜索条件 $sr->do_search();
$sr->set_find; $sr->do_search(); //下面是定制的返回信息
header(‘Content-Type: text/plain’); echo ‘发现和替换以下几个地方:
‘.$sr->get_num_occurences().”rn”; echo
‘啊,错误发生如下………….: ‘.$sr->get_last_error().”rn”;
?> //将以下文字存为test.txt,注意text.txt必须是可读可写的
“我非常喜欢asp,它简单易学,功能强,听说asp已经占了大半市场,asp真好。”
此时,如果您打开exampe.php 就会出现下面这些: 发现和替换以下几个地方:3
啊,错误发生如下……….:查看test.txt文件,果然出现asp的地方被php替换了。

为此,我们将创建一个HTML表格,将接受用户文本,也让用户可以看到从
MySQL数据库,其中包含在过去输入的文本生成的标签云。

 代码如下

<?php
 echo ‘<form method=”post” action=”tag_cloud_gen.php”
name=”gen_tag_db”>’;
 echo ‘<p>Input your text here:<br /><textarea
name=”tag_input” rows=”20″ cols=”80″></textarea></p>’;
 echo ‘<input type=”submit” name=”submit”>’;
 echo ‘</form>’;
?>
<br />
<h3>OR</h3>
<br />
<p>see the current tag cloud here</p>
<?php
 echo ‘<form name=”show_tag_cloud” method=”post”
action=”show_tag_cloud.php”>’;
 echo ‘<input type=”submit” value=”show current tag cloud” >’;
 echo ‘</form>’;
?>

其中每个计算其频率和对将进入一个数组,输入的文本将被表征为单个词。然后将这个数组存储到一个MySQL数据库,我们可以选择保存在MySQL数据库表coloumn存储任何链接,如果这个项目未来的扩展。

1) tag_id —- int,primary key,auto increament 1)tag_id –
整型,主键,自动increament

2) keyword — varchar(20),unique
2)关键字 – 数据类型为varchar(20),独特的

3) weight — int 3)重量 – 诠释

4) link — varchar(256). 4)链接 – 为varchar(256)。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website