2015-05-13 5 views
2

Мне нужно создать сайт с помощью php, который может искать слово и возвращать строку, содержащую слово из 3 введенного текста, который был исправлен. Я могу найти слово и проверить, существует ли он внутри входного текста. Как я могу найти и вернуть результат в строку, содержащую словоСтрока поиска и строка возврата, содержащая строку

вот некоторые данные из входного файла

<paragraph><Date><Date>TUESDAY</Date></Date>. <Date>MARCH 3, 1903</Date> 
Outstation Prosperity. 
</paragraph> 
<paragraph>THE perusal of the various Annual Reports from Outstations is interesting: and very minutely shows the work of the Country. 
</paragraph> 

здесь мои кодирования, что я сделал

<?php 
$path = 'D:\Collective Intelligence\assignment&project\PROJECT-DATASET'; 
$findThisString = $_POST['search1']; 

$dir = dir($path); 

// Get next file/dir name in directory 
while (false !== ($file = $dir->read())) 
{ 
    if ($file != '.' && $file != '..') 
    { 
     // Is this entry a file or directory? 
     if (is_file($path . '/' . $file)) 
     { 
      // Its a file, yay! Lets get the file's contents 
      $data = file_get_contents($path . '/' . $file); 

      // Is the str in the data (case-insensitive search) 
      if (stripos($data, $findThisString) !== false) 
      { 
       // sw00t! we have a match 
      echo 'match found in ' . $file . "<br>\n"; 

      } 
     } 
    } 
} 

$dir->close(); 

?> 

ответ

0

Вы можете использовать fgets, чтобы проверить те линии, которые вы хотите. Просто добавьте простой счетчик:

if (is_file($path . '/' . $file)) { 
    // Its a file, yay! Lets get the file's contents 
    $data = file_get_contents($path . '/' . $file); 

    $fh = fopen($path . '/' . $file, 'r'); 
    $i = 1; 
    while(!feof($fh)) { 
     $line = fgets($fh); 
     if(stripos($line, $findThisString) !== false) { 
      echo "match {$findThisString} found in line {$i}<br/>\n"; 
     } 
     $i++; 
    } 
    fclose($fh); 
} 
+0

спасибо, что вы лучший! =) – user2947488

+0

@ user2947488 уверен, что это помогло – Ghost

Смежные вопросы