Jump to content

summary content before search keyword in text


artablog

Recommended Posts

I am using the below code to truncate my content before and after the first search keyword in my text

(this is for my search page)

function neatest_trim($content, $chars, $searchquery,$characters_before,$characters_after) {
    if (strlen($content) > $chars) {
    $pos = strpos($content, $searchquery);
    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    $start = strpos($content, " ", $start);
    $len = $pos + strlen($searchquery) + $characters_after - $start;
    $content = str_replace(' ', ' ', $content);
    $content = str_replace("\n", '', $content);
    $content = strip_tags(trim($content));
    $content = preg_replace('/\s+?(\S+)?$/', '', mb_substr($content, $start, $len));
    $content = trim($content) . '...';
    $content = strip_tags($content);
    $content = str_ireplace($searchquery, '<b>' . $searchquery . '</b>', $content);
    }
    return $content;
  }


$content = '
lients at the centre of the relationship and to offer a first class service to them which includes tax planning investment management and estate planning. We believe that our customer focused and


';
$searchquery = 'the centre includes';
echo neatest_trim($content,0,$searchquery,0,50);

example my finded post with below keyword :

 

the centre includes

 

 

in below string :

 

lients at the centre of the relationship and to offer a first class service to them which includes tax planning investment management and estate planning. We believe that our customer focused and

 

 

and i want to show below output:

 

at the centre of the relationship and to offer... includes investment management and the estate planning...

 

How can I change this function to get this output?

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.