请先在text.txt文件里面写东西.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <style type="text/css"> #center{ width:300px; border:1px solid #ccc; height:auto; margin:0 auto; text-align:center; color:#ff6666; font-weight:normal; } #content p{ width:300px; text-indent:20px; border:1px solid #ccc; margin-bottom:3px; padding:5px 5px 5px 5px; font-weight:normal; } #showTime{ color:#99cc66; } #area{ margin-bottom:10px; } #input{ margin-top:10px; } #text{ width:300px; height:auto; line-height:30px; border:1px solid #ccc; font-weight:normal; font-size:14px; text-indent:20px; color:#339933; padding-top:10px; padding-bottom:10px; padding-left:10px; padding-right:10px; } </style> </head> <body> <div id="header">统计关键字在文章中出现的次数</div> <div id="content"> <center>请输入查询关键字:</center> <center> <form action="cyg.php" method="post"> <input type='text' value='' name='str' size=15><br> <input type="submit" value="查 询" id='input'> </form> <div id='text'> <?php echo $text=file_get_contents('text.txt');?> </div> </center> <?php if(!empty($_POST['str'])){ $str = $_POST['str']; echo "<script>alert('".$str."关键字出现的次数为:".substr_count($text,$str)."');</script>"; } ?> </div> </body> </html>