limit text length in php and provide 'Read more' link

php limit string to 50 characters
substr php
php limit string length
php string length
word limit in php
php get last character of string
limit text length in html and provide 'read more' link
limit text length css

I have text stored in the php variable $text. This text can be 100 or 1000 or 10000 words. As currently implemented, my page extends based on the text, but if the text is too long the page looks ugly.

I want to get the length of the text and limit the number of characters to maybe 500, and if the text exceeds this limit I want to provide a link saying, "Read more." If the "Read More" link is clicked, it will show a pop with all the text in $text.

This is what I use:

// strip tags to avoid breaking any html
$string = strip_tags($string);
if (strlen($string) > 500) {

    // truncate string
    $stringCut = substr($string, 0, 500);
    $endPoint = strrpos($stringCut, ' ');

    //if the string doesn't contain any space then it will cut without word basis.
    $string = $endPoint? substr($stringCut, 0, $endPoint) : substr($stringCut, 0);
    $string .= '... <a href="/this/story">Read More</a>';
echo $string;

You can tweak it further but it gets the job done in production.

How to limit the number of characters allowed in form input text field?, Yes you limit text and provide "read more" but you dont provide collapse onclick LOL. Read Duration: 3:28 Posted: May 29, 2017 Home PHP How to limit text length in php and provide ‘Read more’ link. PHP; How to limit text length in php and provide ‘Read more’ link. 0. WhatsApp. Facebook.

$num_words = 101;
$words = array();
$words = explode(" ", $original_string, $num_words);
$shown_string = "";

if(count($words) == 101){
   $words[100] = " ... ";

$shown_string = implode(" ", $words);

PHP, Notes ¶. Note: strlen() returns the number of bytes rather than the number of characters in a string. function limit_text( $text, $limit = 100000000000 ) { if ( strlen  How we can limit text length in php and provide 'read more' link. 5 views Sometimes we are required to “read more link” after some characters when we have the long text and limited space.

I have combine two different answers:

  1. Limit the characters
  2. Complete HTML missing tags

    $string = strip_tags($strHTML);
    $yourText = $strHTML;
    if (strlen($string) > 350) {
        $stringCut = substr($post->body, 0, 350);
        $doc = new DOMDocument();
        $yourText = $doc->saveHTML();
    $yourText."...<a href=''>View More</a>"

how to limit text length in php and provide read more link, limit text length in php and provide 'Read more' link. php get string after character php get last character of string php limit string to 50 characters substr in php php​  How to create array from comma separated string in php Often times when coding in PHP, we have the need to turn an array into a comma separated string. try using explode function

Simple use this to strip the text :

echo strlen($string) >= 500 ? 
substr($string, 0, 490) . ' <a href="link/to/the/entire/text.htm">[Read more]</a>' : 

Edit and finally :

function split_words($string, $nb_caracs, $separator){
    $string = strip_tags(html_entity_decode($string));
    if( strlen($string) <= $nb_caracs ){
        $final_string = $string;
    } else {
        $final_string = "";
        $words = explode(" ", $string);
        foreach( $words as $value ){
            if( strlen($final_string . " " . $value) < $nb_caracs ){
                if( !empty($final_string) ) $final_string .= " ";
                $final_string .= $value;
            } else {
        $final_string .= $separator;
    return $final_string;

Here separator is the href link to

strlen - Manual, Limit string length and display '' <?php // Limit $summary to how many characters? $limit = 100; $summary = <<< summary Text Text Text Text Text Text Text  how to limit text length in php and provide read more link.

<?php $string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
if (strlen($string) > 25) {
$trimstring = substr($string, 0, 25). ' <a href="#">readmore...</a>';
} else {
$trimstring = $string;
echo $trimstring;
//Output : Lorem Ipsum is simply dum [readmore...][1]

limit text length in php and provide 'Read more' link, For example, you could use CSS, JavaScript, or do it through PHP. Limiting strings is such a common use case that Laravel provides a simple  How to limit text length in php and provide 'Read more' link - by Kingsolomon - 10-31-2018, 02:08 PM RE: How to limit text length in php and provide 'Read more' link - by Shugudun - 04-14-2020, 12:43 AM

PHP substr() Function, This php script helps you limit displaying characters form you message and not cut out your word. Syntax. substr($message, start, length);. Example 1. <?php $  Get the width and height of an image using a function in PHP PHP provides a function to get the width and height of an image. The getimagesize() function will determine the size of image file including flash file(swf).

Limit string length and display '' : String Length « String « PHP, Truncating text string using PHP. Limiting echo output to a certain length, breaking on spaces or full stop. Limiting or Truncating strings using PHP. Here's a simple An alternative solution has been provided by Bakaburg (see Feedback). Returns the extracted part of a string, or FALSE on failure, or an empty string: PHP Version: 4+ Changelog: PHP 7.0 - If string = start (in characters long), it will return an empty string. Earlier versions returns FALSE. PHP 5.2.2 - 5.2.6 - If start has the position of a negative truncation, FALSE is returned. Other versions get the string

Using str_limit to restrict a string to a certain length, html() methods to truncate the paragraphs of a text and add read more link at the end, if the number of characters inside a paragraph exceeds a certain length. String length validation is measuring the length of a string entered into a text box and making sure that it doesn't go under the minimum length or that it doesn't exceed the maximum length. It is important because let's say, for example, you have members sign up on your website, you may want to limit the length of the username and password a

  • can there be markup in $text?
  • Thanks for the Answer , but after using strip_tags($strings) , the html is lost , how can I after reducing the string length retain the html back which was previously present
  • @harshal The strip_tags is in there because most sites I've worked on just want plain text blurbs (that then link to the page with the fully formatted article). If you want to retain the HTML you can use an XML parser to grab the text values of each sub-element, accumulating the character count while also checking for possible tag breakage. That is a much more complex problem and well beyond the scope of this question.
  • @Webbiedave what is the href here?? sorry i did not understand it thats why i asked?? should it point to self page or something else?
  • "make sure it ends in a word so assassinate doesn't become ass... " ROFL
  • Three dots will be wont be added to a result if the first statement in a ternary check is true. So, brackets should be added around ternary check $teaser = ($endPoint ? substr($stringCut, 0, $endPoint) : substr($stringCut, 0)) . ' ...'; Beside that, it's nice solution.
  • you can change the "..." to a link to more information, or make it open a div with the whole block of text on the same page. Lots of options..
  • please format your answer next time. there are appropriate buttons at the top of the textarea
  • As this is a formatting issue, it's preferred to limit based on character count rather than word count as words will be of varying length.
  • @stillstanding: Thanks, I didn't realize it has removed the returns until after I had posted. I assumed it would be shown as typed.
  • You DO realize that is 500 LETTERS, not words, rite? :D Also, this can completely break the code of the side, if there is any HTML code (can result like <im instead of <img> ...)
  • Ok so use strip_tags() inside ;)
  • @Aif I edit my function withe the split_words function. Hope this help :)