preg_replace的问题和? (问号) - 怎么办?

时间:2010-10-06 20:11:42

标签: php search similarity

我已经制作了这一行代码来使得这些单词更加强大。

$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.';
}

1 个答案:

答案 0 :(得分:1)

您可以在preg_quote()上运行$searchstr函数,该函数将在字符串用作模式之前转义任何元字符。

例如:

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