0

検索対象の単語を強化するために、このコード行を作成しました。

$tekst = preg_replace("/($searchstr)/i", '<span style="color: 8fb842; font-weight: bold;">$1</span>', $tekst);

しかし、私の問題は、 $searchstr = '?'; を作成するときです。$tekst 文字列のすべての文字の間に設定されています。

スクリプト全体は次のとおりです。

/////////////////////////////////
// Set variables and arrays
/////////////////////////////////
$arr_sim = array();

if( !empty($_GET['search']) )
{
    $text=strtolower($_GET['search']); 
    $code_entities_replace = array(' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','~','`','=', 'Æ', 'æ', '&aelig;', 'ø', 'Ø', '&oslash;', 'å', '&aring;', 'Å'); 
    $code_entities_match = array('-','--','','_quot_','_s_','_s-a_','_sq_','procent','_tria_','_and_','_star_','_par-st_','_par-end_','_us_','_plus_','_tub-st_','_tub-end_','_line_','_col_','_anf_','_lt_','_st_','_qm_','_sqpar-s_','_sqpar-e_','_backsla2_', '_semcol_','_1anf_','_comma_','_punc_','_slash_','_nearequ_', '_msql-anf_', '_equal_', 'ae', 'ae', 'ae', 'oe', 'oe', 'oe', 'aa', 'aa', 'aa'); 
    $text = str_replace($code_entities_match, $code_entities_replace, $text);
    $searchstr = $text;
    $search_str = $showpages->escape_str($text);

    $showpages->_table = 'pages';
    $showpages->_where = " tekst LIKE '%". $search_str . "%' AND language_id = '" . $_SESSION['lang_id']. "'";
    $showpages->_orderby = 'id';
    $showpages->_order = 'desc';

    $f = $showpages->search();

    while ( $row = mysql_fetch_array($f) )
    {
        $arr_text = explode(" ",$row['tekst']);
        $percent = 0;
        $words = count($arr_text);
        foreach( $arr_text as $k => $v )
        {
            similar_text($search_str, $v, $p);
            $percent += $p;
        }
        $percent = $percent / $words;

        $arr_sim[] = array('percent' => $percent, 'id' => $row['id']);
    }
}

/////////////////////////////////
// Set some variables
/////////////////////////////////
$arr_len = count($arr_sim);
if( !empty($_GET['offset']) )
    $offset = $showpages->escape_str($_GET['offset']);
else
    $offset = 0;

if( $arr_len >= 15 )
{
    $limit = 15;
} else {
    $limit = $arr_len;
}
$start = $offset+$limit;

rsort($arr_sim); // Sort the array to make the script show in the right order.
/////////////////////////////////


///////////////////
// Show content
////////////////////

if( $arr_len > 0 )
// check if the search is performed and any hits were found.
{
    for( $i = $offset; $i < $start; $i++)
    {
        // $arr_sim[$i]['id']
        $showpages->_table  = 'pages';
        $showpages->_id     = $arr_sim[$i]['id'];
        $showpages->_orderby = 'id';
        $showpages->_order  = 'desc';
        $z = $showpages->search();
        $r = $showpages->fetch_array( $z );

        $tekst = strip_tags($r['tekst'], "<p><a>");
        $str_pos = strpos($tekst, $searchstr);
        if( $str_pos > 50 ){
            $start_str = '...';
            if(strlen($tekst) > 200)
            {
                $end_str = '...';
            }
            $tekst = substr($tekst, $str_pos-50, 200);
        } else {
            $start_str = '';
            if(strlen($tekst) > 200)
            {
                $end_str = '...';
            }
            $tekst = substr($tekst, 0, 200);
        }

        $tekst = preg_replace("/($searchstr)/i", '<span style="color: 8fb842; font-weight: bold;">$1</span>', $tekst);

        $dataa .= '<style type="text/css">.search-fr { margin-bottom: 15px; } .search-fr a {padding: 0; margin: 0 0 5px 0; font-weight: bold; font-size: 14px;} .search-fr p { padding: 0; margin: 0;}</style>';
        $dataa .= '<div class="search-fr">';
        $dataa .= '<a href="' . HTTP_SITE_ROOT . DIRECTORY_SEPERATOR . $r['metashorttag'] . '" class="link2">' . $r['emne'] . '</a> - Matcher din søgning <b>'. round($arr_sim[$i]['percent']).'%</b><br />';
        $dataa .= $start_str.$tekst.$end_str;
        $dataa .= '</div>';
    }
} else {
    $dataa .= 'Der er ikke fundet nogle sider, der matcher din søgning. Prøv igen.';
}
4

1 に答える 1

1

文字列がパターンとして使用される前に、メタ文字をエスケープするpreg_quote()関数を実行できます。$searchstr

例えば:

$tekst = preg_replace('/'.preg_quote($searchstr, '/').'/i', '<span style="color: 8fb842; font-weight: bold;">$1</span>', $tekst);
于 2010-10-06T20:22:06.127 に答える