artablog Posted December 3, 2016 Share Posted December 3, 2016 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted December 3, 2016 Share Posted December 3, 2016 You'll have to be more precise: you know what the output should be but why is it? What decisions did you make to arrive at that output? If you can explain what you did then you can express it as code. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2016 Share Posted December 3, 2016 I think it's a contest. The first person to work out what his input was wins the prize. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.