Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use hack of Chris_O for highlighting the search terms. When a user enters the search keywords without any blank space in the beginning or at end of the search string it retrieves and highlights the search terms in the corresponding records nicely.

But if the user enters the search string with blank_space in the beginning or at the end, it retrieves and highlights the the search terms but all non-english portions of the post content appears weird (like ���� or ????). Although the english content appears fine but with tags (e..g, <p style="text-align: center;">test site</p> etc.)

Is it possible in the hack to strip/remove all blank spaces at the beginning/end of a search string? thanks a lot.

I use version 3.1.4 with the default twentyten theme.

Here is the Hack:

Step 1:

Add these 2 functions to your functions.php

function search_content_highlight()
{
    $content = get_the_content();
    $keys = implode('|', explode(' ', get_search_query()));
    $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">'placeholder4'</strong>', $content);
    echo '<p>' . $content . '</p>';
}

function search_title_highlight()
{
    $title = get_the_title();
    $keys = implode('|', explode(' ', get_search_query()));
    $title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">'placeholder4'</strong>', $title);
    echo $title;
}

Step 2:

In my theme's loop.php file I call <?php search_title_highlight(); ?> instead of <?php the_title(); ?> and use <?php search_content_highlight(); ?> instead of <?php the_content(); ?>

Step 3: In my css add the search-highlight class

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

i agree w/ chip, you need trim()

i'd try replacing:

$keys = implode('|', explode(' ', get_search_query()));

with

$keys = implode('|', explode(' ', trim(get_search_query())));

share|improve this answer
    
thanks a lot. it worked. Also thanks to Chip. –  Tara Jul 1 '11 at 16:50
add comment

Use the PHP function trim().

share|improve this answer
    
Bennett, thanks for the reply. Chip please tell me how to use it. Thanks a lot. –  Tara Jul 1 '11 at 15:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.