Jump to content

hi,everyone,i am new here,i have a question!


Tertius

Recommended Posts

i am writing highlighting function for the keywords of search engine,

 

for example ,i type "1" in the search bar, it pops out the locations of webpages that contain "1",

 

and then when I click the webpage.

 

it will search all the data I choose from database at that page,

 

for example, i want it to show some contents from database, so i type echo $array[content] something like that.

 

those data is from database, but

 

it will also search the html code inside the $array[content],

 

and highlight them.

 

for example, i have a article.

 

 

<p><br><font size="12px">Abc</font> and def are are friend,they meet together at 1997.</p>

 

this is the article in the database,this is the data inside the $array[content],

 

and I search the "1"

 

and replaced it with  $replacement = "<font class=\"searchhighlight\">$searchterm</font>";

 

 

which is a css.

 

its css code is like this.

 

 

.searchhighlight {background-color:#99CC99; color:#000099; font-style:italic;}

 

 

,i make it this way,

$arraycontent = str_replace("$searchterm","$replacement","$array[content]");

 

and then, i want it to replace the "1" inside the sentense,

 

Sentense is "they meet together at 1997."

 

the "1" inside 1997.

 

but it will also, highlight the "1" inside the html code,so it will be like this.

 

 

<p><br><font size="<font class=\"searchhighlight\">1</font>2px">Abc</font> and def are are friend,they meet together at <font class=\"searchhighlight\">1</font>997.</p>

 

so,now i only want it to show like this, print the code of this:

 

 

<p><br><font size="12px">Abc</font> and def are are friend,they meet together at <font class=\"searchhighlight\">1</font>997.</p>

 

can you tell me how will you guys make it become this way?

 

Link to comment
Share on other sites

try

 

<?php

function highlight_search_string ($art, $srch)
{
    $replace = sprintf('<span class="searchhighlight">%s</span>', $srch);

    $newtext = '';

    $len = strlen($art);
    $str = '';

    for ($i=0; $i < $len; $i++)
    {
        switch ($c = $art{$i})
        {
            case '<':
                        if (strpos($str, $srch) !== false) $str = str_replace($srch, $replace,$str);
                        $newtext .= $str;
                        $str = $c;
                        break;
            case '>':
                        $str .= $c;
                        $newtext .= $str;
                        $str = '';
                        break;
            default:
                        $str .= $c;
                        break;
        }

    }
    if (strpos($str, $srch) !== false)  $str = str_replace($srch, $replace,$str);
    $newtext .= $str;

    return $newtext;
}

/**
* Call the function
*/
$article = '<p>
<font size="12px">Abc</font> and def are are friend,they meet together at 1997.</p>';

$search = '1';


echo highlight_search_string($article, $search);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.