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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:48:07+00:00 2026-06-09T10:48:07+00:00

Here is my code, what im doing is pulling text from a database, displaying

  • 0

Here is my code, what im doing is pulling text from a database, displaying in a table and where the data exceeds lets say 450 characters i put this on the end

….[view more]

Now the code works fine but there is one exception, the information in the database has html in it, like paragraphs and bullet lists. That poses a problem, the whole idea of putting a limit is so it doesnt stretch the row down further than i want it to go, a line break for a bullet list or a paragraph seems to be counted as 0 or 1 charatcers but it takes up the space of a lot of characters so how can i manipulate this code so that linebreaks are accounted for.

My ideas are to count the whitespace between with something like this:

$white_space = substr_count($text, ' ');

Which returns the total whitespace

I also tried this

$white_space_str = substr_count($newstr, ' ');

But that returns 0 so im doing something wrong.
But in any case im a bit stuck at this point and hoping someone can help out a newbie, if the code is simplified rather than trimmed and neat it might help me understand it better 🙂

But im not sure how to put that into a working code.

function trim_description($str, $maxlen) {
if ( strlen($str) <= $maxlen ) return $str;

$newstr = substr($str, 0, $maxlen);
if ( substr($newstr,-1,1) != ' ' ) $newstr = substr($newstr, 0, strrpos($newstr, " "));

return $newstr;
}
  • 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-09T10:48:09+00:00Added an answer on June 9, 2026 at 10:48 am

    Maybe this can help you. I found this as an answer for this question

    function truncate($text, $length, $suffix = '&hellip;', $isHTML = true) { 
        $i = 0; 
        $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true); 
        $tags = array(); 
        if($isHTML){ 
            preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); 
            foreach($m as $o){ 
                if($o[0][1] - $i >= $length) 
                    break; 
                $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1); 
                // test if the tag is unpaired, then we mustn't save them 
                if($t[0] != '/' && (!isset($simpleTags[$t]))) 
                    $tags[] = $t; 
                elseif(end($tags) == substr($t, 1)) 
                    array_pop($tags); 
                $i += $o[1][1] - $o[0][1]; 
            } 
        } 
    
        // output without closing tags 
        $output = substr($text, 0, $length = min(strlen($text),  $length + $i)); 
        // closing tags 
        $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : ''); 
    
        // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">) 
        $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE))); 
        // Append closing tags to output 
        $output.=$output2; 
    
        // Get everything until last space 
        $one = substr($output, 0, $pos); 
        // Get the rest 
        $two = substr($output, $pos, (strlen($output) - $pos)); 
        // Extract all tags from the last bit 
        preg_match_all('/<(.*?)>/s', $two, $tags); 
        // Add suffix if needed 
        if (strlen($text) > $length) { $one .= $suffix; } 
        // Re-attach tags 
        $output = $one . implode($tags[0]); 
    
        //added to remove  unnecessary closure 
        $output = str_replace('</!-->','',$output);  
    
        return $output; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code which is doing the conversion from hex to decimal. The
Here is how I'm populating my GridView control. I'm doing this from the code-behind,
I'm pulling a field from a table and doing different things depending on what
So here's what I see this code doing: An array is made A loop
I am doing a simple communication between sockets and here is my C++ code
What am I doing wrong here? Declare @starttimestamp datetime = getdate(); RAISERROR(N'Code not valid
Here my code, I need to insert into mysql database I have mysql,php,android 1)
Here is code from MSDN . I don't understand why the work isn't just
I'm importing data in a SQL Server 2008 database from excel file where the
I'm runing a query to pull some movie data from a source table to

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.