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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:36:02+00:00 2026-05-16T10:36:02+00:00

I would like to insert a read more HTML bit at a certain position

  • 0

I would like to insert a read more HTML bit at a certain position in an HTML fragment – say after the 10th word. I will then, via JavaScript, hide the text after the “read more” and only show it on click.

The JavaScript part is ok. As to the PHP part.

It seemed simple at first – but the problem is that tags mess up the word count and the truncation. So I would need to close any opened tag which closing counterpart is beyond the “read more”.

Any insight would be appreciated.

  • 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-16T10:36:02+00:00Added an answer on May 16, 2026 at 10:36 am

    I don’t know exactly where this source is from but I’ve been using this piece of code, it’s excellent.

    /**
    * @desc Cut given plain/HTML text nicely
    * @param string text to cut
    * @param int approximetly length of desired text length
    * @param int optional length, how far text can variante from approximetly length
    * @param bool optional can we cut words
    * @param bool optional do we need to append three dots to the end of cutted text
    * @return string cutted text
    */
    
    function htmlSubstr($text, $approxLength, $lengthOffset = 20, $cutWords = FALSE, $dotsAtEnd = TRUE) {
        mb_internal_encoding('UTF-8');
        // $approxLength:
        // The approximate length you want the concatenated text to be
    
        // $lengthOffset:
        // The variation in how long the text can be in this example text
        // length will be between 200 and 200-20=180 characters and the
        // character where the last tag ends
        // Reset tag counter & quote checker
        $tag_counter = 0;
        $quotes_on = FALSE;
    
        // Check if the text is too long
        if (mb_strlen($text) > $approxLength) {
            // Reset the tag_counter and pass through (part of) the entire text
            $c = 0;
            for ($i = 0; $i < mb_strlen($text); $i++) {
                // Load the current character and the next one
                // if the string has not arrived at the last character
                $current_char = mb_substr($text,$i,1);
                if ($i < mb_strlen($text) - 1) {
                    $next_char = mb_substr($text,$i + 1,1);
                } else {
                    $next_char = "";
                }
    
                // First check if quotes are on
                if (!$quotes_on) {
                    // Check if it's a tag
                    // On a "<" add 3 if it's an opening tag (like <a href...)
                    // or add only 1 if it's an ending tag (like </a>)
                    if ($current_char == '<') {
                        if ($next_char == '/') {
                            $tag_counter += 1;
                        } else {
                            $tag_counter += 3;
                        }
                    }
    
                    // Slash signifies an ending (like </a> or ... />)
                    // substract 2
                    if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;
                    // On a ">" substract 1
                    if ($current_char == '>') $tag_counter -= 1;
                    // If quotes are encountered, start ignoring the tags
                    // (for directory slashes)
                    if ($current_char == '"') $quotes_on = TRUE;
                } else {
                    // IF quotes are encountered again, turn it back off
                    if ($current_char == '"') $quotes_on = FALSE;
                }
    
                // Count only the chars outside html tags
                if($tag_counter == 2 || $tag_counter == 0) $c++;
    
                // Check if the counter has reached the minimum length yet,
                // then wait for the tag_counter to become 0, and chop the string there
                if ($c > $approxLength - $lengthOffset && $tag_counter == 0 && ($next_char == ' ' || $cutWords == TRUE)) {
                    $text = mb_substr($text,0,$i + 1);
                    if($dotsAtEnd){
                        $text .= '...';
                    }
    
                    return $text;
                }
            }
        }
        return $text;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should be able to do this in a slightly… May 16, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer Not as far as I know. Dojo's stateful relies on… May 16, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer this element.onclick = function () { // this == element… May 16, 2026 at 4:52 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

I would like to find out if someone (and maybe someone on StackOverflow works
I'm noticing this error more and more in my error logs. I've read through
Quite a funny question I have. I am working now on the HTML parser
UPDATE: OK I figured it out, looks like fread has a filesize limitation, changed
I cant believe this, i always throught the below would be concurrency safe. I
I have read some tutorials for bezier curve such as this one http://www.codeproject.com/KB/recipes/BezirCurves.aspx .
I have some code that looks like this: std::set<int> s1, s2, out; // ...
I have a Rails site, where the content is written in markdown. I wish
For real-time multimedia tasks the low latency is essential. A stable low latency value
Am in the process of implementing a fairly conventional shopping checkout flow. Currently it

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.