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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:37:37+00:00 2026-06-09T16:37:37+00:00

I have the following function to truncate text: /** * Removes HTML tags, crops

  • 0

I have the following function to truncate text:

 /**
     * Removes HTML tags, crops to 255 symbols
     *
     * @param string $unformatted
     */
    public function formatShortDescr($unformatted) {
        if(strlen($unformatted)<1) return;

        $long_text = strip_tags(trim($unformatted));
        $max_length = 255;

        if(strlen($long_text) > $max_length){
            $short_text = (substr($long_text,0,$max_length));
        } else {
            $short_text = $long_text;
        }
        return $short_text;
    }

E.g this:
<p>Victory har utvecklats f&ouml;r att passa den &auml;gare som beh&ouml;ver en kompakt, ........ get converted into: Victory har utvecklats f&ouml;r att passa den &a

How can I set it to never cut a string half way through break the html entities?

  • 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-06-09T16:37:38+00:00Added an answer on June 9, 2026 at 4:37 pm

    should be easy to first convert the entities to normal chars, then use mb_strlen (becaus of 2-byte characters, UTF8) to check the length and mb_substring to truncate and THEN convert entities back…

        $long_text = strip_tags(trim($unformatted));
        $long_text = html_entity_decode($long_text);
    
        $long_text = mb_strlen($long_text) > $max_length ? mb_substr($long_text, 0, $max_length) : $long_text;
    
        return htmlentities($long_text);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following function in my code public List<string> GetpathsById(List<long> id) { List<string> paths
I have the following function: function truncate($string, $limit, $break=., $pad=...) { if(strlen($string) <= $limit)
I have the following function: public DataTable GetRecordSet(String query, string[] parameters) It is typically
I have following function in one Activity public void AppExit() { Editor edit =
I have the following function using Excel 2010: Private Function MakeAllSheetsValuesOnly(targetBookName As String) If
I have the following function inside my HomeController class: public class HomeController : Controller
I have the following function: <script type=text/javascript> function changeText(elem){ var oldHTML = document.getElementById(elem).innerHTML; var
Have following kernel function: private static String programSource = __kernel void sampleKernel(__global float *Y,
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return

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.