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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:24:11+00:00 2026-05-30T06:24:11+00:00

Given an HTML string, I would like to return a modified string with the

  • 0

Given an HTML string, I would like to return a modified string with the following properties:

  1. The first n characters of the text contents (HTML tags aside) should remain.
  2. Elements after n characters have been met should be removed entirely.
  3. If n characters is not at the end of an element, text afterwards in the same element should not remain.
  4. Tags on elements at and before n characters should remain.

Basically, I just want to return a shortened version of the HTML, without the DOM structure being interrupted, and based on the length of the text contents only.

Using PHP’s DOM implementation, it seems this will be overly complex. Using a pattern match isn’t ideal as the conditions of the modified string might change over time, and it would require rewriting each time.

Am I missing an easier way of doing this? Thanks in advance.

  • 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-30T06:24:12+00:00Added an answer on May 30, 2026 at 6:24 am

    “Using PHP’s DOM implementation, it seems this will be overly complex.”

    Really?

    Here’s a very simple DOM implementation if you want the first 100 characters from inside the <body> tag and its child nodes. You could further massage this to remove newline characters and superfluous space/tab characters or check the length of the $content string inside the foreach to break the loop and stop concatenation once you’ve reached a certain number of characters.

    $str = '...';
    $dom = new DomDocument;
    $dom->loadHTML($str);
    $elements = $dom->getElementsByTagName('body');
    
    $content = '';
    foreach($elements as $node){
      foreach($node->childNodes as $child) {
        $content .= $child->nodeValue;
      }
    }
    
    echo substr($content, 0, 100);
    

    UPDATE

    As per your comment, here’s a simple way to count the characters inside HTML nodes and delete all the tags after the specified character limit is reached. Note that you can’t perform the delete operation inside the original foreach because it causes DOM to reindex the nodes and you won’t get the results you expect. Instead, we store the nodes we want to delete in an array and delete them after the initial iteration.

    $str = '...';
    $dom = new DomDocument;
    $dom->preserveWhitespace = FALSE;
    $dom->loadHTML($str);
    
    $elements = $dom->getElementsByTagName('body');
    
    $remove   = FALSE;
    $maxChars = 100;
    $content  = '';
    $delete   = array();
    
    foreach($elements as $node){
      foreach($node->childNodes as $child) {
        if ($remove) {
          $delete[] = $child;
        } else {
          $content .= $child->nodeValue;
          if ( ! $remove && strlen($content) >= $maxChars) {
            $remove = TRUE;
          }
        }
      }
    }
    
    foreach ($delete as $child) {
      $child->parentNode->removeChild($child);
    }
    
    $dom->formatOutput = TRUE;
    echo $dom->saveHTML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to check if a given string has a correct html syntax.
Given an HTML string like: <span class=findme id=31313131313>The Goods</span> What kind of REGEX in
Basically, I would like to decode a given HTML document, and replace all special
I have a necessity to sort a given HTML table of the following structure,
Given an HTML page that has a complex table-based layout and many tags that
Given this HTML: <div>foo</div><div>bar</div><div>baz</div> How do you make them display inline like this: foo
Given an HTML link like <a href=urltxt class=someclass close=true>texttxt</a> how can I isolate the
I have the following Java code to fetch the entire contents of an HTML
I have a string that represents a non indented XML that I would like
I'm trying to write a highlight plugin, and would like to preserve HTML formatting.

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.