0

i have a PHP code that read multiple TXT files and allow the user to make a search wher he enter a string and the system read all files and display the result in a table where it contains the filename and line number.

screenshot: enter image description here

what i need now is to make the system read MS publisher files and display the same result.

is this possible?

code:

<?php

$result = [];
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;
        $temp   = [];
        $myFileLink = fopen($txts, 'r');
        while(!feof($myFileLink))
        {
            $myFileContents = fgets($myFileLink);
            if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
            {

                $temp['filename'] = basename ($txts);
                foreach($matches[1] as $match)
                {
                    $temp['lines'][] = $line;
                }
            }
            ++$line;
        }
        fclose($myFileLink);

        $result[] = $temp;
    }

    //display the table
    echo '<table class = "minimalistBlack" border=2>';

 $filenameHtml    = '<tr>';
 $lineNumberHtml    = '<tr>';
 foreach ($result as $item)
 {
    $filename = isset($item['filename']) ? $item['filename'] : '';
    $lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
//$filenameHtml .= "<th>$filename</th>";
    $new_filename = str_replace('.txt', '.pdf',$filename);
    $filenameHtml .= !empty($filename) ? "<th><a href ='".$new_filename."'target='_blank'>$filename</a></th>" : ''; // added !empty()
    $lineNumberHtml .= !empty($lines) ? "<td>$lines</td>" : ''; // added !empty()
  }
    $filenameHtml    .= '</tr>';
    $lineNumberHtml  .= '</tr>';

    echo $filenameHtml.$lineNumberHtml;
    echo '</table>';
}
?>

<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>
Ghgh Lhlh
  • 155
  • 1
  • 3
  • 14
  • Have you tried changing `$_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt"` to `$_SERVER['DOCUMENT_ROOT']."/readfiletest/*.pub"`? – Geshode Jan 09 '18 at 08:14
  • @Geshode yes i have tried it did not display anything because its another extension and maybe another encode – Ghgh Lhlh Jan 09 '18 at 08:23
  • If you open a .pub file in your text editor. Does it contain pure text? My bet, is that these files contain encoded data, and thus plain text search wouldn't work. – Ole Haugset Jan 09 '18 at 09:23
  • @OleHaugset what i am doing is save the .PUB as .txt in order to search inside the files... now i want to read directly from the .PUB files ============when i save as .txt the displaying of the content is changed with no good format. – Ghgh Lhlh Jan 09 '18 at 09:30

0 Answers0