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 42789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:23:35+00:00 2026-05-10T15:23:35+00:00

I have a code snippet written in PHP that pulls a block of text

  • 0

I have a code snippet written in PHP that pulls a block of text from a database and sends it out to a widget on a webpage. The original block of text can be a lengthy article or a short sentence or two; but for this widget I can’t display more than, say, 200 characters. I could use substr() to chop off the text at 200 chars, but the result would be cutting off in the middle of words– what I really want is to chop the text at the end of the last word before 200 chars.

  • 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. 2026-05-10T15:23:35+00:00Added an answer on May 10, 2026 at 3:23 pm

    By using the wordwrap function. It splits the texts in multiple lines such that the maximum width is the one you specified, breaking at word boundaries. After splitting, you simply take the first line:

    substr($string, 0, strpos(wordwrap($string, $your_desired_width), "\n")); 

    One thing this one-liner doesn’t handle is the case when the text itself is shorter than the desired width. To handle this edge-case, one should do something like:

    if (strlen($string) > $your_desired_width)  {     $string = wordwrap($string, $your_desired_width);     $string = substr($string, 0, strpos($string, "\n")); } 

    The above solution has the problem of prematurely cutting the text if it contains a newline before the actual cutpoint. Here’s a version which solves this problem:

    function tokenTruncate($string, $your_desired_width) {   $parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);   $parts_count = count($parts);    $length = 0;   $last_part = 0;   for (; $last_part < $parts_count; ++$last_part) {     $length += strlen($parts[$last_part]);     if ($length > $your_desired_width) { break; }   }    return implode(array_slice($parts, 0, $last_part)); } 

    Also, here is the PHPUnit test class used to test the implementation:

    class TokenTruncateTest extends PHPUnit_Framework_TestCase {   public function testBasic() {     $this->assertEquals("1 3 5 7 9 ",       tokenTruncate("1 3 5 7 9 11 14", 10));   }    public function testEmptyString() {     $this->assertEquals("",       tokenTruncate("", 10));   }    public function testShortString() {     $this->assertEquals("1 3",       tokenTruncate("1 3", 10));   }    public function testStringTooLong() {     $this->assertEquals("",       tokenTruncate("toooooooooooolooooong", 10));   }    public function testContainingNewline() {     $this->assertEquals("1 3\n5 7 9 ",       tokenTruncate("1 3\n5 7 9 11 14", 10));   } } 

    Special UTF8 characters like ‘à’ are not handled. Add ‘u’ at the end of the regex to handle it:

    $parts = preg_split('/([\s\n\r]+)/u', $string, null, PREG_SPLIT_DELIM_CAPTURE);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't really know how to define how secure it… May 11, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer I've used svn on a shared network directory between unix,… May 11, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer There's no specification per se. The closest thing is the… May 11, 2026 at 11:37 pm

Related Questions

What I am asking, is what is the best way (i.e. the way that
In an application I work on, any business logic error causes an exception to
For a user object in my asp.net mvc project, I have written a custom
I am writing a library in VB.NET in which I have added, among others,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.