Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6742457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:50:44+00:00 2026-05-26T11:50:44+00:00

I am using the below code to truncate my content before and after the

  • 0

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) everything works as it should apart from the code cutting words in half at the beginning of the truncate, it doesn’t cut words at the end of the truncate.

Example:

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...

(edit:it is sometimes more than one character missing from the word)

You will see that it has chopped the ‘c’ off ‘clients’. It only happens at the beginning of the text not the end. How can I fix this? I believe I am half way there. code so far:

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;
                $len = $pos + strlen($searchquery) + $characters_after - $start;
                $content = str_replace('&nbsp;', ' ', $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, '<span class="highlight" style="background: #E6E6E6;">' . $searchquery . '</span>', $content);
            }
            return $content;
        }



 $results[] = Array(
  'text' => neatest_trim($row->content,200,$searchquery,120,80)
            );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:50:45+00:00Added an answer on May 26, 2026 at 11:50 am

    The 120 Characters that you are keeping at the start don’t check if the 120th character is a space or a letter, and just cuts the string there no matter what.

    I would make this change, to search for the closest “space” to the position we are starting from.

    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    // add this line:
    $start = strpos($content, ' ', $start);
    $len = $pos + strlen($searchquery) + $characters_after - $start;
    

    This way $start is the position of a space, and not a letter from a word.

    Your Function would become:

    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('&nbsp;', ' ', $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, '<span class="highlight" style="background: #E6E6E6;">' . $searchquery . '</span>', $content);
        }
        return $content;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to Add SummaryLinkWebPart to a Page and also adding
i am using below code to print my content of webpage in asp.net using
Using below code, I am getting this error: NSTask: Task create for path '/Users/media/Library/Developer/Xcode/DerivedData/Manager-gnixeptcszdoubbnfcncumzypwge/Build/Products/Debug/lib'
I'm using the code below to convert meters to feet. It works like a
I am using below code to send email it works fine most of the
I am using below code to get a page: $.get('http://example.com/page1.html', function (data) { });
I am using below code to send a file via blue-tooth from one device
I am using below code to get calendar events in my application. It's working
I am using below code to generate random numbers in range... int randomNumberWithinRange(int min,int
I am using below code to to create Contact Service and to Validate login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.