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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:48:32+00:00 2026-06-06T06:48:32+00:00

I’m working in PHP and I want to create a function that, given a

  • 0

I’m working in PHP and I want to create a function that, given a text of arbitrary length and height, returns a restricted version of the same text with a maximum of 500 characters and 10 lines.

This is what I have so far:

function preview($str)
{
    $partialPreview = explode("\n", substr($str, 0, 500));
    $partialPreviewHeight = count($partialPreview);
    $finalPreview = "";

    // if it has more than 10 lines
    if ($partialPreviewHeight > 10) {
        for ($i = 0; $i < 10; $i++) {
            $finalPreview .= $partialPreview[$i];
        }
    } else {
        $finalPreview = substr($str, 0, 500);
    }

    return $finalPreview;
}

I have two questions:

  • Is using \n proper to detect new line feeds? I know that some
    systems use \n, other \r\n and others \r, but \n is the most
    common.
  • Sometimes, if there’s an HTML entity like &quot; (quotation mark) at
    the end, it’s left as &quot, and therefore it’s not valid HTML. How
    can I prevent this?
  • 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-06T06:48:33+00:00Added an answer on June 6, 2026 at 6:48 am

    Is using \n proper to detect new line feeds? I know that some systems use \n, other \r\n and others \r, but \n is the most common.

    It depends where the data is coming from. Different operating systems have different line breaks.

    Windows uses \r\n, *nix (including mac OS) uses \n, (very) old macs used \r. If the data is coming from the web (e.g. a textarea) it will (/ should) always be \r\n. Because that’s what the spec states user agents should do.

    Sometimes, if there’s an HTML entity like " (quotation mark) at the end, it’s left as &quot, and therefore it’s not valid HTML. How can I prevent this?

    Before cutting the text you may want to convert html entities back to normal text. By using either htmlspecialchars_decode() or html_entity_decode depending on your needs. Now you won’t have the problem of breaking the entities (don’t forget to encode it again if needed).

    Another option would be to only break the text on whitespace characters rather than a hard character limit. This way you will only have whole words in your “summary”.

    I’ve created a class which should deal with most issues. As I already stated when the data is coming from a textarea it will always be \r\n, but to be able to parse other linebreaks I came up with something like the following (untested):

    class Preview
    {
        protected $maxCharacters;
        protected $maxLines;
        protected $encoding;
        protected $lineBreaks;
    
        public function __construct($maxCharacters = 500, $maxLines = 10, $encoding = 'UTF-8', array $lineBreaks = array("\r\n", "\r", "\n"))
        {
            $this->maxCharacters = $maxCharacters;
            $this->maxLines = $maxLines;
            $this->encoding = $encoding;
            $this->lineBreaks = $lineBreaks;
        }
    
        public function makePreview($text)
        {
            $text = $this->normalizeLinebreaks($text);
    
            // this prevents the breaking of the &quote; etc
            $text = html_entity_decode($text, ENT_QUOTES, $this->encoding);
    
            $text = $this->limitLines($text);
    
            if (mb_strlen($text, $this->encoding) > $this->maxCharacters) {
                $text = $this->limitCharacters($text);
            }
    
            return html_entity_decode($text, ENT_QUOTES, $this->encoding);
        }
    
        protected function normalizeLinebreaks($text)
        {
            return str_replace($lineBreaks, "\n", $text);
        }
    
        protected function limitLines($text)
        {
            $lines = explode("\n", $text);
            $limitedLines = array_slice($lines, 0, $this->maxLines);
    
            return implode("\n", $limitedLines);
        }
    
        protected function limitCharacters($text)
        {
            return substr($text, 0, $this->maxCharacters);
        }
    }
    
    $preview = new Preview();
    echo $preview->makePreview('Some text which will be turned into a preview.');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.