Jump to content

Recommended Posts

Hi All,

 

I am trying to create a search system - in which part of it I would like to highlight the matched terminology from the relative $_POST value that is coming from the search box.

 

The SQL that I am using to get the query results is:

 

select * from queue where id like '%$searchvalue%' or subject like '%$searchvalue%' or body like '%$searchvalue%'

 

This returns any rows that include like $searchvalue. So if $searchvalue was say "linux" - I would like the code to highlight the word Linux on the page. I am displaying the results on the page using an MSSQL_FETCH_ARRAY, but only displaying the ID and subject fields.

 

Hope that makes sense?

 

Thanks

Matt

Link to comment
https://forums.phpfreaks.com/topic/225475-highlighting-_post-value-in-text/
Share on other sites

you could use str_replace after you get your results like this like this

 

$searchvalue=str_replace("$search" ,"<font color='#FF0000'>$search</font>", $searchvalue);

 

this would then change the color of the variable $search if that represents the word looked for.

Hi dragon_sa,

 

Thanks for that code - however I am finding that it is highlighting the whole line.

 

Let me post my code:


$criteria = $_POST ['search_text'];
$search_query = mssql_query("select id,subject,body from table where id like %'$criteria'% or subject like %'$criteria'% or body like %'$criteria'%"

$show_results = mssql_fetch_assoc ($search_query ){

$show_results['subject']=str_replace("$show_results['subject']" ,"<font color='#FF0000'>$show_results['subject']</font>", $show_results['subject']);

echo $show_results['id'];
echo $show_results['subject'];
echo $show_results['body'];
}

 

Cheers

Matt

I made a multi-word multi-color highlighter

http://dynaindex.com/color-highlighter.php

 

http://www.phpfreaks.com/forums/php-coding-help/highlight-a-word-using-php/msg1516715/#msg1516715

 

Is 2  diferent functions there at above link, the one i linked is for when are single values in an array, the other function below it is for when need to explode a line a text for the words, exclude anything, and then create the array.

 

You can look there but here's an even newer version of the code I did.

 

function highlighter($words, $content, $backgroundcolors=null, $fontcolors=null) {
preg_match_all('~\w+~', $words, $matched);
    if(!$matched) {
        return $content;
       }
$words = str_replace(array('+','-'), "", $words); 
$words = str_replace(" ", "|", $words);
$words = trim($words);
$words = explode("|", $words);
        if(is_null($backgroundcolors) || !is_array($backgroundcolors)) {
                $backgroundcolors = array('yellow', 'red', 'green', 'orange', 'aqua', 'lightskyblue ', 'pink', 'lime', 'fuchsia', 'maroon', 'navy', 'olive', 'purple', 'gray', 'silver', 'teal', 'grey');
                if(is_null($fontcolors) || !is_array($fontcolors)) {
                $fontcolors = array('black', 'white', 'lime', 'oldlace');//change font colors or add more
                }
        }
        $counting = 0;
        $numbercolors = max(array_keys($backgroundcolors));
        foreach ($words as $word) {
            $word = preg_quote(trim($word));               
$content = preg_replace("/\b($word)\b/i", '<span style="border: solid 2px #FFD700; padding: 1px; -moz-border-radius: 5px; -khtml-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; margin: -0; text-align: justify; line-height: 2px; background-color:'.$backgroundcolors[$counting].';font-family:arial;color:'.$fontcolors[$counting].';">\1</span>', $content);
                if($counting==$numbercolors){ $counting = 0; } else { $counting++; }
        }
        
        return $content;
}
?>

 

a simple usage

$post_content = highlighter($search_words, $post_content);

 

You can define the colors also, look at examples the demo or codes other forum post.

 

I had css style in this at first in my included stylesheet, then modified the css class into the output themselves. Any time a css class met another class and was a hyperlink there.....it messed up the hyper, was still a pretty color though, so I redid it with good old depreciated basics of font.

this

$subject=str_replace($criteria ,"<font color='#FF0000'>$criteria</font>", $subject);

would become

$subject=str_ireplace($criteria ,"<font color='#FF0000'>$criteria</font>", $subject);

 

this is the case insensitive version :)

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.