Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:41:36+00:00 2026-05-17T15:41:36+00:00

I have made this line of code to make the words, that is searced

  • 0

I have made this line of code to make the words, that is searced for, enhanced.

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

But my problem is, that when I make $searchstr = ‘?’; it is setting between every letter in the $tekst string.

The whole script is:

/////////////////////////////////
// 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:41:37+00:00Added an answer on May 17, 2026 at 3:41 pm

    You can run the preg_quote() function on $searchstr which will escape any metacharacters before the string is used as your pattern.

    For example:

    $tekst = preg_replace('/'.preg_quote($searchstr, '/').'/i', '<span style="color: 8fb842; font-weight: bold;">$1</span>', $tekst);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made this so far. It's code that will make a json String
I have a line, which is a sprite made by using this code CGPoint
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
Hi have made this function which is made to replicate an error that I
I have made this code to open a database(created in SQLite browser) stored in
I am new to ZF.i have made a function that basically make a Form
I have made this centered navigation bar, and I wanted to add a :active
I have made this webpage, you can check it out here http://www.bettingtowin.net/webcam.html I have
OK, I was just fooling around in my spare time and have made this
Hi I have recently made this script to rename files I scan for work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.