Тема: Модуль поиска, Ошибка в раскладке при поиске

Требуется ли сайту поиск для тех кто по ошибке о печатался в поисковой форме?

Например изменить вывод поиска добавив так:

Ничего не найдено по запросу «ctujlyz».
Попробуйте изменить параметры поиска.

Может вы искали «сегодня»
Всего найдено стр. по запросу «сегодня» :
Ошибки SEO /stati/oshibki_seo.html
от Search Engine Optimization) это одно из наиболее эффективных на сегодня средств раскрутки сайта, представляющее собой тонкую настройку разных элементов веб-сайта для того, чтобы привлечь пользователей поисковых машин.


Изменил файл поиска "search.php"
Добавил такую функцию:

function correctString($string)//Неправильная раскладка клавиатуры
    {
    
     $search =  array(
      "/q/iu","/w/iu","/e/iu","/r/iu","/t/iu","/y/iu","/u/iu","/i/iu","/o/iu","/p/iu","/\[/iu","/\]/iu",
      "/a/iu","/s/iu","/d/iu","/f/iu","/g/iu","/h/iu","/j/iu","/k/iu","/l/iu","/\;/iu","/\'/iu",
      "/z/iu","/x/iu","/c/iu","/v/iu","/b/iu","/n/iu","/m/iu","/,/iu","/\./iu"
      );
      $replace = array(
      "й","ц","у","к","е","н","г","ш","щ","з","х","ъ",
      "ф","ы","в","а","п","р","о","л","д","ж","э",
      "я","ч","с","м","и","т","ь","б","ю"
      );
      
      return preg_replace($search, $replace, $string);
    }

Полный код страницы.


<?php

function safestr($str){
    $sfs=trim($str);
    return str_replace("\n","<br />",str_replace("\r\n","<br />", htmlspecialchars(strip_tags($sfs))));
}



function correctString($string)//Неправильная раскладка клавиатуры
    {
    
     $search =  array(
      "/q/iu","/w/iu","/e/iu","/r/iu","/t/iu","/y/iu","/u/iu","/i/iu","/o/iu","/p/iu","/\[/iu","/\]/iu",
      "/a/iu","/s/iu","/d/iu","/f/iu","/g/iu","/h/iu","/j/iu","/k/iu","/l/iu","/\;/iu","/\'/iu",
      "/z/iu","/x/iu","/c/iu","/v/iu","/b/iu","/n/iu","/m/iu","/,/iu","/\./iu"
      );
      $replace = array(
      "й","ц","у","к","е","н","г","ш","щ","з","х","ъ",
      "ф","ы","в","а","п","р","о","л","д","ж","э",
      "я","ч","с","м","и","т","ь","б","ю"
      );
      
      return preg_replace($search, $replace, $string);
    }






$searching=@safestr($_POST['searchstring']);
echo '<form id="mySearchForm" name="mySearch" action="search.html" method="post">
<table class="feedback" cellspacing="4">
    <tr>
        <td>'; if (!empty($searching)) echo '
            <input id="query-search" type="text" style="width:300px;padding:4px" name="searchstring" value="'.$searching.'">';
else echo '<input id="query-search" type="text" style="width:300px;padding:4px" name="searchstring" value="">';
        echo '</td>
        <td><input type="submit" style="width:160px;padding:4px" value="Поиск по страницам" /></td>
    </tr>
</table>
</form><br />';

echo '<form id="mySearchForm" name="mySearch" action="search-news.html" method="post">
<table class="feedback" cellspacing="4">
    <tr>
        <td>'; if (!empty($searching)) echo '
            <input id="query-search" type="text" style="width:300px;padding:4px" name="searchstring" value="'.$searching.'">';
else echo '<input id="query-search" type="text" style="width:300px;padding:4px" name="searchstring" value="">';
        echo '</td>
        <td><input type="submit" style="width:160px;padding:4px" value="Поиск в новостях" /></td>
    </tr>
</table>
</form><br />';


$searchingcorrect = correctString($searching);



function searching($search){

    if(!empty($search)){
        $arr = ROOT_PATH.'articles';
        $dir = scandir($arr,1);
        $arr2 = str_replace("/articles",'',$arr);
        for ($i = 0; $i < count($dir)-2; $i++) {
            if( preg_match("/\.dat$/i",$dir[$i])) {
                if(preg_match("/(^404)\.dat/i",$dir[$i]))continue;
                if(file_exists($arr.'/'.$dir[$i])) {
                    $fh = @fopen($arr.'/'.$dir[$i], 'r');
                    $data = fread($fh, filesize($arr.'/'.$dir[$i]));
                    fclose($fh);
                    $seach_page = preg_replace('/\.dat$/i','.html',$dir[$i]);
                    $page = "/$seach_page";
                    unset($count, $content);
                    $data= preg_replace("'<style[^>]*?>.*?</style>'si", "", $data);
                    $data= preg_replace("'<script[^>]*?>.*?</script>'si", "", $data);
                    $data= preg_replace("'<head[^>]*?>.*?</head>'si", "", $data);
                    $data= preg_replace("|<meta[^>]+>|si", "", $data);
                    $read_bak=$data;
                    $data= preg_replace( "'&(nbsp|#160);'i", " ", $data);
                    $data= preg_replace( "'([\r\n])[\s]+'", " ", $data);
                    $text = explode('<!-- Kan_title -->',$data);
                    @$title=$text[1];
                    $text = explode('<!-- Kan_content -->',$data);
                    @$content .= $text[1];
                    $content= strip_tags($content);
                    $arr_searching=explode(" ",$search);
                    unset($relevant);
                    foreach($arr_searching as $line) {
                        $pattern="/$line/i";
                        @$count+=preg_match_all($pattern,$content,$found);
                        @$relevant+=preg_match_all($pattern,$content,$found);
                        $count+=preg_match_all($pattern,$title,$found);
                        $relevant+=preg_match_all($pattern,$title,$found);
                    }
                    if($count){
                        @$count_query_res++;
                        $read_bak= strip_tags($read_bak,"<br /><p><font><b><i><u><pre><table><td><tr><h1><h2><h3><h4><h5>");
                        $read_bak= preg_replace( "'&(nbsp|#160);'i", " ", $read_bak);
                        $read_bak= preg_replace( "'([\r\n])[\s]+'", " ", $read_bak);
                        foreach($arr_searching as $line) {
                            if(strpos($read_bak,$line)===false) $line=strtolower($line);
                            if(strpos($read_bak,$line)===false) $line=ucfirst($line);
                            if(strpos($read_bak,$line)===false) $line=ucwords($line);
                            if(strpos($read_bak,$line)===false) $line=strtoupper($line);
                            $read_bak=  str_replace($line,"<span id=select>$line</span>", $read_bak);
                        }
                        @$count_bak++;
                        foreach($arr_searching as $line) {
                            if(strpos($content,$line)===false) $line=strtolower($line);
                            if(strpos($content,$line)===false) $line=ucfirst($line);
                            if(strpos($content,$line)===false) $line=ucwords($line);
                            if(strpos($content,$line)===false) $line=strtoupper($line);
                            $word_pos= strpos($content,$line);
                            if($word_pos!==false) break;
                        }
                        $to_word = substr($content, 0, $word_pos);
                        $start = strrpos($to_word, ".");
                        if ($start != 0) $start++; else $start=0;
                        $to_stop = substr($content, $word_pos);
                        $stop = strpos($to_stop, ".") + 1 + $word_pos;
                        if(strpos($content,".",$word_pos)===false)$stop=strlen($content)+1;
                        $content=substr($content,$start,(++$stop-$start));
                        foreach($arr_searching as $line) {
                            $pattern="/($line)/i";
                            $replacement="<b><font color=red>\$1</font></b>";
                            $content=preg_replace($pattern, $replacement, $content);
                            $title=preg_replace($pattern, $replacement, $title);
                        }
                        $link=substr_replace(@$dirname,"",0,2);
                        if($title=="") $title="Документ без названия";
                        if($seach_page!="main.html") {$link1="<a href=$seach_page>$title</a>"; $link2="/$seach_page";}
                        else {$link1="<a href=/>$title</a>"; $link2="/";}
                        if($seach_page!="404.html"){
                            $output[@$founded][$relevant] = ' '.$link1.'
                                <span>'.$link2.'</span>
                                <span><div>'.$content.'</div></span><br />';
                            $rel[@$founded] = $relevant;
                            @$founded++;
                        }
                    }
                }
            } else {
                if(preg_match("/(^\.)|(\.[a-z]*)/i",$dir[$i]))continue;
                if(file_exists($arr.'/'.$dir[$i].'/main.dat')) {
                    $tempdir=scandir($arr.'/'.$dir[$i],1);
                    for ($j = 0; $j < count($tempdir)-2; $j++) {
                        if( preg_match("/\.dat$/i",$dir[$i].'/'.$tempdir[$j])) {
                            if(file_exists($arr.'/'.$dir[$i].'/'.$tempdir[$j])) {
                                $fh = @fopen($arr.'/'.$dir[$i].'/'.$tempdir[$j], 'r');
                                $data = fread($fh, filesize($arr.'/'.$dir[$i].'/'.$tempdir[$j]));
                                fclose($fh);
                                $seach_page = preg_replace('/\.dat$/i','.html',$tempdir[$j]);
                                $page = "$dir[$i]/$seach_page";
                                //echo($page."<br />");
                                unset($count, $content);
                                $data= preg_replace("'<style[^>]*?>.*?</style>'si", "", $data);
                                $data= preg_replace("'<script[^>]*?>.*?</script>'si", "", $data);
                                $data= preg_replace("'<head[^>]*?>.*?</head>'si", "", $data);
                                $data= preg_replace("|<meta[^>]+>|si", "", $data);
                                $read_bak=$data;
                                //$data= strip_tags($data);
                                $data= preg_replace( "'&(nbsp|#160);'i", " ", $data);
                                $data= preg_replace( "'([\r\n])[\s]+'", " ", $data);
                                //echo htmlspecialchars($data).'<br />';
                                $text = explode('<!-- Kan_title -->',$data);
                                @$title=$text[1];
                                $text = explode('<!-- Kan_content -->',$data);
                                @$content .= $text[1];
                                $content= strip_tags($content);
                                //echo $title.'<br />'.$content;
                                $arr_searching=explode(" ",$search);
                                unset($relevant);
                                foreach($arr_searching as $line) {
                                    $pattern="/$line/i";
                                    @$count+=preg_match_all($pattern,$content,$found);
                                    @$relevant+=preg_match_all($pattern,$content,$found);
                                    $count+=preg_match_all($pattern,$title,$found);
                                    $relevant+=preg_match_all($pattern,$title,$found);
                                }
                                //echo ($title.'<br />'.$pattern.'<>'.$content.'<>'.$count.'<br /><br /><br />');
                                if($count){
                                    @$count_query_res++;
                                    $read_bak= strip_tags($read_bak,"<br /><p><font><b><i><u><pre><table><td><tr><h1><h2><h3><h4><h5>");
                                    $read_bak= preg_replace( "'&(nbsp|#160);'i", " ", $read_bak);
                                    $read_bak= preg_replace( "'([\r\n])[\s]+'", " ", $read_bak);
                                    foreach($arr_searching as $line) {
                                        if(strpos($read_bak,$line)===false) $line=strtolower($line);
                                        if(strpos($read_bak,$line)===false) $line=ucfirst($line);
                                        if(strpos($read_bak,$line)===false) $line=ucwords($line);
                                        if(strpos($read_bak,$line)===false) $line=strtoupper($line);
                                        $read_bak=  str_replace($line,"<span id=select>$line</span>", $read_bak);
                                    }
                                    @$count_bak++;
                                    foreach($arr_searching as $line) {
                                        if(strpos($content,$line)===false) $line=strtolower($line);
                                        if(strpos($content,$line)===false) $line=ucfirst($line);
                                        if(strpos($content,$line)===false) $line=ucwords($line);
                                        if(strpos($content,$line)===false) $line=strtoupper($line);
                                        $word_pos= strpos($content,$line);
                                        if($word_pos!==false) break;
                                    }
                                    $to_word = substr($content, 0, $word_pos);
                                    $start = strrpos($to_word, ".");
                                    if ($start != 0) $start++; else $start=0;
                                    $to_stop = substr($content, $word_pos);
                                    $stop = strpos($to_stop, ".") + 1 + $word_pos;
                                    if(strpos($content,".",$word_pos)===false)$stop=strlen($content)+1;
                                    $content=substr($content,$start,(++$stop-$start));
                                    foreach($arr_searching as $line) {
                                        $pattern="/($line)/i";
                                        $replacement="<b><font color=red>\$1</font></b>";
                                        $content=preg_replace($pattern, $replacement, $content);
                                        $title=preg_replace($pattern, $replacement, $title);
                                    }
                                    @$link=substr_replace(@$dirname,"",0,2);
                                    if($title=="") $title="Документ без названия";
                                    if($seach_page!="main.html") {$link1="<a href=/$dir[$i]/$seach_page>$title</a>"; $link2="/$dir[$i]/$seach_page";}
                                    else {$link1="<a href=/$dir[$i]/>$title</a>"; $link2="/$dir[$i]/";}
                                    if($seach_page!="404.html"){
                                        $output[@$founded][$relevant] = ' '.$link1.'
                                            <span>'.$link2.'</span>
                                            <span><div>'.$content.'</div></span><br />';
                                        $rel[@$founded] = $relevant;
                                        
                                        @$founded++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }    
    return array("rel"=>$rel,"out"=>$output);
}    
    
    
    
    echo '<div name="result_local" number="1120"><div class="gridwidth_4" style="margin-top:1em;">';
    if(empty($founded)) {
        echo '<div><span><div><span><b>Ничего не найдено по запросу «<font color=red>'.$searching.'</font>».<br />Попробуйте изменить параметры поиска.</b></span></div></span></div>';
        
        echo '<div><span><b>Может вы искали «<font color=red>'.$searchingcorrect.'</font>»</b></span></div>';
        
    $relatcorrect_arrray = searching($searchingcorrect);
    
    $relatcorrect = array_unique($relatcorrect_arrray['rel']);
    rsort($relatcorrect);
    reset($relatcorrect);
        echo '<div><span><div><span><b>Всего найдено '.$founded.' стр. по запросу «<font color=red>'.$searchingcorrect.'</font>» :</b></span></div></span></div><div id="mysearch"><div class="gridwidth_4" style="margin-top:1em;">';
        foreach($relatcorrect as $key => $val) {
            foreach($relatcorrect_arrray['out'] as $ke => $va){
                foreach($va as $k => $v){
                    if($k == $val) echo $v;
                }
            }
        }
        echo '</div></div>';
        
        
        echo '</div></div>';
    } else {
    $relat_array = searching($searching);
    $relat = array_unique($relat_array['rel']);
    rsort($relat);
    reset($relat);
        echo '<div><span><div><span><b>Всего найдено '.$founded.' стр. по запросу «<font color=red>'.$searching.'</font>» :</b></span></div></span></div><div id="mysearch"><div class="gridwidth_4" style="margin-top:1em;">';
        foreach($relat as $key => $val) {
            foreach($relat_array['out'] as $ke => $va){
                foreach($va as $k => $v){
                    if($k == $val) echo $v;
                }
            }
        }
        echo '</div></div></div></div>';
    }

?>