Function to show read more link in php

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Function to show read more link in php

    FUNCTION TO SHOW READ MORE LINK IN PHP.
    DONT FORGET TO CLICK THANKS. ;) :p
    Have you ever came across a situation where you need to truncate the large text and show the "read more.." link to redirect user to read the full post ?
    I know you might be searching for a function to show read more link in php.

    Here is a simple function which can be used to show the "Read more.." links dynamically.

    PHP Code:
    <?php  
    //function to truncate text and show read more link  
    function truncate($mytext,$link,$var,$id) {  
    //Number of characters to show  
    $chars 25;  
    $mytext substr($mytext,0,$chars);  
    $mytext substr($mytext,0,strrpos($mytext,' '));  
    $mytext $mytext." <a href='$link?$var=$id'>read more...</a>";  
    return 
    $mytext;  
    }  
    ?>

    The above function can be used anywhere to show read more links with dynamic mysql queries.

    How to use this function ???
    Here is a simple example to do this
    First you will need to create the connection with the database.
    Connection settings are stored in "connect.php" file

    PHP Code:
    <?php  
    include "connect.php";  
    $sql "SELECT * FROM articles";  
    $result mysql_query($sql);  
    while(
    $row mysql_fetch_array($result))  
    {  
    echo 
    "\n";  
    echo 
    truncate($row['article_text'],"article.php","article_id",$row['article_id']);  
    }  
      
    //function to truncate text and show read more link  
    function truncate($mytext,$link,$var,$id) {  
    //Number of characters to show  
    $chars 25;  
    $mytext substr($mytext,0,$chars);  
    $mytext substr($mytext,0,strrpos($mytext,' '));  
    $mytext $mytext." <a href='$link?$var=$id'>read more...</a>";  
    return 
    $mytext;  
    }  
    ?>
    This will output your article text with proper linking to original post with read more link.
    Even you can customize the truncate function for more security check and customizing your display.

    #2
    Thank for sharing
    Last edited by nimeshka; 10.12.15, 11:17.


    I'm Proud to be a Sri Lankan!

    Comment


      #3
      Originally posted by nimeshka View Post
      PHP Code:
      include ("connect.php"); 
      Wat do you mean by that?
      Silently dropping by an useless piece of code.
      Last edited by softwarefreak; 13.09.12, 04:59.
      I need some facebook likes, can you please help me
      http://facebook.com/softwarefreakin
      I noticed social media is really powerful
      Well DONE is better than well SAID

      Comment


        #4
        Thanks for that tkreturns!

        I just recently had a need for something like that and ended up using JavaScript instead.

        Added after 3 minutes:

        Originally posted by nimeshka View Post
        PHP Code:
        include ("connect.php"); 
        No parenthesis are needed.
        Last edited by Budove; 13.09.12, 12:34.

        Comment


          #5
          Originally posted by Budove View Post
          Thanks for that tkreturns!

          I just recently had a need for something like that and ended up using JavaScript instead.

          Added after 3 minutes:



          No parenthesis are needed.
          Cheers mate. Here to help. Drop a message if you need any help

          Comment


            #6
            thanks for this it was lovely

            PHP Code:
            /**
             * Limit the number of characters in a string.
             *
             * <code>
             *        // Returns "Tay..."
             *        echo strlimit('Taylor', 3);
             *
             *        // Limit the number of characters and append a custom ending
             *        echo strlimit('Taylor', 3, '---');
             * </code>
             *
             * @param  string  $value
             * @param  int     $limit
             * @param  string  $end
             * @return string
             */
            function strlimit($value$limit 100$end '...')
            {
                return (
            strlen($value) <= $limit) ? $value substr($value0$limit).$end;
            }

            /**
             * Limit the number of chracters in a string including custom ending
             * 
             * <code>
             *        // Returns "Taylor..."
             *        echo strlimit_exact('Taylor Otwell', 9);
             *
             *        // Limit the number of characters and append a custom ending
             *        echo strlimit_exact('Taylor Otwell', 9, '---');
             * </code>
             * 
             * @param  string  $value
             * @param  int     $limit
             * @param  string  $end
             * @return string
             */
            function strlimit_exact($value$limit 100$end '...')
            {
                
            $limit -= strlen($end);

                return (
            strlen($value) <= $limit) ? $value strlimit($value$limit$end);
            }

            /**
             * Limit the number of words in a string.
             *
             * <code>
             *        // Returns "This is a..."
             *        echo strlimit_words('This is a sentence.', 3);
             *
             *        // Limit the number of words and append a custom ending
             *        echo strlimit_words('This is a sentence.', 3, '---');
             * </code>
             *
             * @param  string  $value
             * @param  int     $words
             * @param  string  $end
             * @return string
             */
            function strlimit_words($value$words 100$end '...')
            {
                if (
            trim($value) == '') return '';

                
            preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u'$value$matches);

                if (
            strlen($value) == strlen($matches[0]))
                {
                    
            $end '';
                }

                return 
            rtrim($matches[0]).$end;

            Comment

            Working...
            X