如何使用PHP

时间:2017-12-31 17:08:32

标签: php html readfile tabular

我有PHP代码从文本文件读取并显示结果,用户输入要搜索的单词,系统读取文件并显示结果,并指定行号和文件名。

我想在表格或形式良好的表格中显示这些结果。

如果可能,列必须是文件名,行包含行号。

代码:

<?php


if(isset($_POST["search"]))
    {
        $search =$_POST['name'];
        echo "the word  $search exist: <br><br>";
        foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
        {
            $line = 1; 
            $myFileLink = fopen($txts, 'r');
            while(!feof($myFileLink)) 
            {
            $myFileContents = fgets($myFileLink);
            if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
            {

                    foreach($matches[1] as $match)
                    {


                         echo "line [$line] file  ";
                    }
                    echo basename ($txts) . "<br>".PHP_EOL;

            }
            ++$line;
            }
        fclose($myFileLink);
        }
    }
?>


<html>
    <head>
    </head>
    <meta http-equiv="Content-Language" content="ar-sa">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
          #form {
        background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        margin: auto;
        width: 200px;
        height: 200px;
        position: absolute;


        font-family: Tahoma, Geneva, sans-serif;
        font-size: 14px;
        font-style: italic;
        line-height: 24px;
        font-weight: bold;
        color: #09C;
        text-decoration: none;
        border-radius: 10px;
        padding: 10px;
        border: 1px solid #999;
        border: inset 1px solid #333;
        -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
      }

    </style
    <body>
    <div id = "form">
        <form action="index.php" method="post">
          <h1 align =center > Search Form </h1>
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p align =center ><input type ="Submit" name ="search" value= "Search" /></p>
        </form>
    </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以使用echo

编写HTML标记

所以,在foreach循环之前写这个。

echo "<table>"

foreach循环内部必须像那样

echo "<tr><td>".$line. "</td></tr>

在foreach循环后关闭表标记

echo "</table>"

就是这样。