Shortening text before full display

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hadge26
    New Member
    • Feb 2008
    • 5

    Shortening text before full display

    Hi Guys,

    Sorry if these are noob questions but i have a few issues with a website im trying to build.

    Ok its a review site, where the user can type their review into a submit page.. got all that working, what i need to know is, is there a way to display say the first 50 words of an article followed by a read more and how do you make a multi page article (although these are possibly the same thing) Just need the names of the commands or something??

    Also I have had to put a meta redirect on my main page into a folder, for some reason code that works in /news/ or /login/ etc will not work on the home page... its got me baffled as it was a copy of one of the other pages with only a few alterations to re-pointing the includes...

    Thanks in advance
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Something like this?
    [php]
    <?php
    $text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
    if (isset($_GET['text']) AND $_GET['text'] == 'all')
    echo $text;
    else {
    echo substr($text,0, 50);
    echo '<br><a href="'.$PHP_SE LF.'?text=all" >read more .....</a>';
    }
    ?>[/php]
    Ronald

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by ronverdonk
      Something like this?
      [php]
      <?php
      $text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
      if (isset($_GET['text']) AND $_GET['text'] == 'all')
      echo $text;
      else {
      echo substr($text,0, 50);
      echo '<br><a href="'.$PHP_SE LF.'?text=all" >read more .....</a>';
      }
      ?>[/php]
      Ronald
      If you dont want to words chopping up, you can use a regexp to check:
      [php]
      $_text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
      if(!$_GET['text'] == "all")
      {
      preg_match('#^\ s*(.{21,}?)\s+. *$#s', $_text, $match);
      echo $match[1];
      }
      else
      {
      echo $_text;
      }
      [/php]
      heres where i got this from.

      Comment

      • hadge26
        New Member
        • Feb 2008
        • 5

        #4
        Originally posted by ronverdonk
        Something like this?
        [php]
        <?php
        $text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
        if (isset($_GET['text']) AND $_GET['text'] == 'all')
        echo $text;
        else {
        echo substr($text,0, 50);
        echo '<br><a href="'.$PHP_SE LF.'?text=all" >read more .....</a>';
        }
        ?>[/php]
        Ronald
        Ronald thats exactly what I mean, i need to implement it so its pulling $text from the database, this is my current code:

        [PHP]<?php $result=mysql_q uery("SELECT * FROM article WHERE cat='hardware' ORDER BY time DESC LIMIT 5");
        while ($row=mysql_fet ch_array($resul t))

        {
        echo "<br /><div class='text'><s pan style='float: left;'><b>{$row["title"]}</b></span><span style='float: right;'><b>Post ed By {$row["poster"]}</b><br />{$row["time"]}</span><br /><br />";
        print "<p>{$row["body"]}<br /></p><span style='float: left;'><b>Categ ory: <a href='{$row["cat"]}.php'>{$row["cat"]}</a></b></span><span style='float: right;'><b>Sour ce: <a href='{$row["link"]}'>{$row["link"]}</a></b></span><br /></div><br />";
        } ?>[/PHP]

        Can you help?

        Comment

        • hadge26
          New Member
          • Feb 2008
          • 5

          #5
          Hey guys,

          I got it working... using a mixture of both suggestions... I'd tell you that ur the best but im guessing thats why you hang around here :)

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            That's right! Pleasure to help you out. See you next time.

            Ronald

            Comment

            Working...