This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: the option does not work

#20681
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

Try this modified version of the code. Enter the keywords to be replaced to the $words variable, comma separated.

add_filter('asp_results', 'asp_remove_first_n_characters');
function asp_remove_first_n_characters($results) {
    $remove = 200;              // Enter the length to cut
    $words = 'word1, word2';    // Enter the words to replace

    // Replace the keywords
    $words = explode(',', $words);
    foreach ( $words as $wk => &$word ) {
        $word = trim($word);
        if ( $word != '' )
            unset($words[$wk]);
    }
    if ( count($words) > 0 ) {
        // implode() words into a pipe-delimited string
        $pattern = "/\b(" . implode("|", $words) . ")\b/i";
        foreach ($results as $k => &$r) {
            $r->content = preg_replace($pattern, "", $r->content);
        }
    }

    // Trim the length
    foreach ($results as $k => &$r) {
        if ( strlen($r->content) > $remove )
            $r->content = substr($r->content, $remove);
    }
    return $results;
}