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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:42:17+00:00 2026-06-10T20:42:17+00:00

This string has 78 characters with HTML and 39 characters without HTML: <p>I really

  • 0

This string has 78 characters with HTML and 39 characters without HTML:

<p>I really like the <a href="http://google.com">Google</a> search engine.</p>

I want to truncate this string based on the non-HTML character count, so for example if I wanted to truncate the above string to 24 characters, the output would be:

I really like the <a href="http://google.com">Google</a>

The truncation did not take into account the html when determining the number of characters to cut off, it only considered the stripped count. However, it didn’t leave open HTML tags.

  • 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-10T20:42:19+00:00Added an answer on June 10, 2026 at 8:42 pm

    Alright so this is what I put together and it seems to be working:

    function truncate_html($string, $length, $postfix = '&hellip;', $isHtml = true) {
        $string = trim($string);
        $postfix = (strlen(strip_tags($string)) > $length) ? $postfix : '';
        $i = 0;
        $tags = []; // change to array() if php version < 5.4
    
        if($isHtml) {
            preg_match_all('/<[^>]+>([^<]*)/', $string, $tagMatches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
            foreach($tagMatches as $tagMatch) {
                if ($tagMatch[0][1] - $i >= $length) {
                    break;
                }
    
                $tag = substr(strtok($tagMatch[0][0], " \t\n\r\0\x0B>"), 1);
                if ($tag[0] != '/') {
                    $tags[] = $tag;
                }
                elseif (end($tags) == substr($tag, 1)) {
                    array_pop($tags);
                }
    
                $i += $tagMatch[1][1] - $tagMatch[0][1];
            }
        }
    
        return substr($string, 0, $length = min(strlen($string), $length + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . $postfix;
    }
    

    Usage:

    truncate_html('<p>I really like the <a href="http://google.com">Google</a> search engine.</p>', 24);
    

    The function was grabbed from (made a small modification):

    http://www.dzone.com/snippets/truncate-text-preserving-html

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

Sidebar

Related Questions

I want to make sure a string has only characters in this range [a-z]
I have a page that has 3 variables. They look like this: String[] Headers
I have a string which has an xml inside. I want to write this
I want to count how many characters a certain string has in PHP, but
I'm using HTMLCleaner to clean an HTML file which has characters like '€' (ascii
I want to give a text to a DIV. This text has HTML tags,
I have some random string, let's say : s = This string has some
I have a variable that has this string: <DIV><SPAN style=FONT-FAMILY: Tahoma; FONT-SIZE: 10pt>[If the
ViM has this option hlsearch where a searched string is displayed in highlight mode
Has anyone ever run into this issue before? Sometimes the string displays, sometimes half

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.