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

  • Home
  • SEARCH
  • 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 569983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:21:11+00:00 2026-05-13T13:21:11+00:00

I am trying to get this method in a String Filter working: public function

  • 0

I am trying to get this method in a String Filter working:

public function truncate($string, $chars = 50, $terminator = ' …');

I’d expect this

$in  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890";
$out = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV …";

and also this

$in  = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝ";
$out = "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđ …";

That is $chars minus the chars of the $terminator string.

In addition, the filter is supposed to cut at the first word boundary below the $chars limit, e.g.

$in  = "Answer to the Ultimate Question of Life, the Universe, and Everything.";
$out = "Answer to the Ultimate Question of Life, the …";

I am pretty certain this should work with these steps

  • substract amount of chars in terminator from maximum chars
  • validate that string is longer than the calculated limit or return it unaltered
  • find the last space character in string below calculated limit to get word boundary
  • cut string at last space or calculated limit if no last space is found
  • append terminator to string
  • return string

However, I have tried various combinations of str* and mb_* functions now, but all yielded wrong results. This can’t be so difficult, so I am obviously missing something. Would someone share a working implementation for this or point me to a resource where I can finally understand how to do it.

Thanks

P.S. Yes, I have checked https://stackoverflow.com/search?q=truncate+string+php before 🙂

  • 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-13T13:21:11+00:00Added an answer on May 13, 2026 at 1:21 pm

    Try this:

    function truncate($string, $chars = 50, $terminator = ' …') {
        $cutPos = $chars - mb_strlen($terminator);
        $boundaryPos = mb_strrpos(mb_substr($string, 0, mb_strpos($string, ' ', $cutPos)), ' ');
        return mb_substr($string, 0, $boundaryPos === false ? $cutPos : $boundaryPos) . $terminator;
    }
    

    But you need to make sure that your internal encoding is properly set.

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

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • 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 If all of the movieclips on the stage had the… May 16, 2026 at 4:06 pm
  • Editorial Team
    Editorial Team added an answer Say you have an element like this: <code id="mycode"></code> You… May 16, 2026 at 4:06 pm
  • Editorial Team
    Editorial Team added an answer Normally the purpose of the upgrade it prevent any data… May 16, 2026 at 4:06 pm

Trending Tags

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

Top Members

Related Questions

What does mean? I get this error when trying to iterate through a file
I am trying to write a method, that takes a ComboBox, a DataTable and
I have a productpart database containing a string property named 'type'. What I'm trying
I am trying to use a fetcher method to retrieve items from my datastore.
I am trying to do PayPal IPN method. So I thought since I am
I'm getting a InvalidOperationException when trying to run this code: database.Produkts.SingleOrDefault<Produkt>(x => x.Kod ==
So I'm trying to get the right Device Context so I can set the
you can't use string.Remove inside a static method because Remove is an instance method.
Been trying to get an answer on what I am doing wrong all over
Good afternoon all, I've got a controller running a before filter with an except

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.