Problem with line breaks in search system

Asked

Viewed 88 times

0

I am developing a search system in PHP without using database and it works very well, except when there are line breaks. How can I fix this?

index php.

<form action="busca.php" method="get">
<input type="text" name="s"><br>
<input type="submit">
</form>

php search.

<?php
$pesq = (isset($_GET['s'])) ? trim($_GET['s']) : '';
if(empty($pesq)) {
echo 'Digite no campo de busca.';
}
else {
$index = "index.php";
$busca = glob("posts/conteudo/*.txt", GLOB_BRACE);
$lendo = "";
$conteudo = "";
foreach($busca as $item) {
if($item !== $index) {
$abrir = fopen($item, "r");
while(!feof($abrir)) {
$lendo = fgets($abrir);
$conteudo .= $lendo;
$lendo .= strip_tags($lendo);
}
if(stristr($lendo, $pesq) == true) {
$dados = str_replace(".txt", "", $item);
$dados = basename($dados);
$titulo = file_get_contents("posts/titulo/$dados.txt");
$result[] = "<a href=\"posts/postagem$dados.php\">$titulo</a>";
unset($dados);
}
fclose($abrir);    
}
}
if(isset($result) && count($result) > 0) {
$result = array_unique($result);
echo '<ul>';
foreach($result as $link) {
echo "<li>$link</li>";
}
echo '</ul>';
}
else {
echo 'Nenhum resultado na busca.<br /> Tente <b><label for="busca">pesquisar por palavras-chave diferentes</label></b> ou <b><label for="google">busque no Google</label></b>.
<br />
<form action="https://www.google.com/search" method="get" target="_blank">
<input type="search" name="q" placeholder="Busca no Google..." id="google" />
</form>';
}
}
?>

For example, if one of the files in which the word "good" is searched has the following "good morning", it works normally and the system can list the file. However, if I look for "good" in a file where there is the "good
day" (with a line break between words), the system does not work and does not list the file.

  • How do line breaks get in the way? How should it be?

  • updated the question, explaining, sorry

  • There its regular expression, must lack the condition for the case of appearing a break after the word.

  • 1

    By the way the line break is ' n' or '<br>'?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.