Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

format search results.

Status
Not open for further replies.

letimati

Technical User
Feb 3, 2006
36
0
0
GB
Hi I want to format the way I output search results by showing the string query inside <em></em> tags and cut it so shows this in the middle of 20 characters.

So if i have

$text = "this is the test text i have but i dont want to output it all so just want to output the important part of the text";

$strignQuery = 'text';

So the output would be

...the test <em>text</em> i have but i... ...part of the <em>text</em>...

My thoughts so far are to string replace the 'text' with '<em>text</em>' easy then i thought if there was a way to do a split it on this with a 10 character on each way then just output the results that would work well. However i am sure there is a more elegant way of doing this.

Thanks in advance for any help


 
This can turn into a complicated topic - would you want to highlight the text even if it is a partial match, e.g. if "text" were inside a word like batextender?

Anyway, I would recommend to use a PCRE (Perl COmpatible Regular Expression) to handle this. Have a look at preg_replace http://www.php.net/preg_replace.

If you need help with the expression, let us know.
 
I would like that to be matched as well.

I have done something like this so far

$noTags = strip_tags($text);
$withEm = str_replace($_GET['sq'], '<em>'.$_GET['sq'].'</em>', $noTags);
print '<p>'.substr($withEm, 0 , 100).'...</p>';
$pos = strrpos($withEm, $_GET['sq']);
if (!($pos === false)){
print '<p>...'.substr($withEm, ($pos - 35), ($pos + 35)).'...</p>';
}

does not really work but I will keep mucking about with it.
 
I had written (a long time ago) a function for highlighting that could be used:
Code:
/**
 * FUNCTION highlight
 * Highlights search terms in an HTML or plain string
 *
 * @param $searchwords string The terms that will be split on spaces into individual words
 * @param $subject string The haystack, may be HTML or plain string
 * 
 * @return $string The manipulated output with all tags reassembled
 */
function highlight($searchwords,$subject) {
        $searchStr = preg_replace('/ /','|',$searchwords);

        /* split the plain text from the HTML */
        $text = preg_split("/([<].*?[>])/",$subject);
        preg_match_all("/([<].*?[>])/",$subject,$tags);

        $result = '';

        /* now replace the searchwords with word+markup */
        foreach ($text as $key=>$value) {
                $text[$key] = preg_replace("/($searchStr)/i",'<span class="highlight">'."\\1".'</span>',$value);
        }

        /* reduce to full matches */
        $tags = $tags[0];

        /* reassemble the string */
        for ($i=0;$i<=count($tags);$i++){
                $result .= $text[$i].$tags[$i];
        }
        return($result);
}
It actually uses CSS markup rather than an <em> tag, but you can easily adapt that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top