读取TXT文件里的内容,生成一个全文字符串,查找要检索的字符串在全文中出现的次数。然后根据回车键把全文字符串分割成数组。然后把检索的内容有在段落里出现的段落罗列出来。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="zh-CN"/>
<title>内容检索</title>
<style type="text/css">
body,table,input,textarea,select {font-family:微软雅黑,Verdana,sans-serif,宋体;}
</style>
</head>
<body>
<form method="post" action="">
输入要检索的内容 :<input type="text" name="str" />
<input type="submit" value="检索" />
</form>
<?php
IF(@$_POST['str']){
$str = $_POST['str'];
$file = "lunyu2.txt";
$content = file_get_contents($file);
function clearBlank($arr)
{
function odd($var)
{
return($var<>'');//return true or false
}
return (array_filter($arr, "odd"));
}
//echo $content;
echo '<br />共检索到 <b>'.substr_count($content, $str).' </b>处';
$arr = clearBlank(explode("\r\n",$content));
$result = array();
foreach($arr as $row){
if(strstr($row,$str)){
$result[] = $row;
}
}
$all = count($result);
echo '<ul>';
for($i=0;$i<$all;$i++){
echo '<li>'.str_replace($str, '<b style="color:red;">'.$str.'</b>',$result[$i]).'</li>';
}
echo '</ul>';
}
?>
</body>
</html>
效果像这样:
至于,lunyu2.txt这个文件内容就是一些段落了~
PS:如果出现取出来的内容全部只有一整段,可以把
$arr = clearBlank(explode("\r\n",$content));
改成:
$arr = clearBlank(explode("\n",$content));
试试! '\r'是回车,'\n'是换行的意思!
2012/09/20 统计文档里每个中文字出现的次数
preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $content, $matches);
$f = join('', $matches[0]); //去除中文外的其它字符
$fr = str_split($f,3); //把字符串转换成数组
$count = count($fr); //统计数组里有多少元素
$cr = array();
for($i=0;$i<$count;$i++){
if(isset($cr[$fr[$i]])){
$cr[$fr[$i]]++; //如果已经在数组里,就加1
}else{
$cr[$fr[$i]] = 1;
}
}
arsort($cr); //按大小排序
$ra = array();
foreach ($cr as $key => $value) {
$ra[] = '<b>'.$key.'</b>:'.$value.'|';
}
$r = implode("",$ra);
echo '<p>文档中出现的字统计:</p>';
echo '<p>'.$r.'</p>';
效果图: