0

可能重复:
PHP 如何截断数组

我有一个显示来自 MySQL 表的文本的数组。我想将数据截断到一定长度(未确定)并在截断末尾添加“...阅读更多”。

这是相关的代码块:

while($rows = mysql_fetch_array($getNews)){
                  $id = $rows['id'];
                  $title = strip_tags(stripslashes($rows['title']));
                  $editor1 = $show->news_article = stripslashes(html_entity_decode($rows['editor1']));
                  $poster = $rows['poster'];
                  $date = $rows['date'];

(该脚本有效并且确实显示了数据,这是整个脚本:)

<?php

function getColour($number){
    switch ($number) {        
    }
    return $colour;
}
// For the header
$colour = 1;

if($_POST['submit']){
    // Clean the submitted form
    $comment = cleanTag($_POST['comment']);
    $name = cleanTag($_POST['name']);
    $tstamp = time();
        $ip = getenv("REMOTE_ADDR");
    $id = cleanTag($_POST['id']);

    if(!$id){
        die("Oops: Unexpected error!");
    }
    if($name == "" || $comment == ""){
        die("You cannot leave any fields blank");
    }
    // Insert the comment
    mysql_query("INSERT INTO comments_articles (comment, name, article, tstamp, ip) VALUES ('{$comment}', '{$name}', '{$id}', '{$tstamp}', '{$ip}')") or die(mysql_error());

    // Tell them the form was submitted
    echo("Your comment has been posted!");

# Insert action log
$date = date("d/m/y - h:ia");
$insertLog = "INSERT INTO `logs` ( `log` , `date` ) VALUES ('<strong>$name ($ip)</strong> has posted a comment<strong>$title</strong>', '$date')";
mysql_query($insertLog) or die('MySQL Error.');

} else if($_GET['id']){
    // Get the id and clean it
    $id = cleanTag($_GET['id']);

    // Get the news and the comments
    $getNews = mysql_query("SELECT * FROM news WHERE category = 1 AND approved = 1 AND id = {$id} ORDER BY id DESC") or die(mysql_error());
    $newsRows = mysql_fetch_array($getNews);

    $getCom = mysql_query("SELECT * FROM comments_articles WHERE article = {$id} ORDER BY tstamp DESC") or die(mysql_error());


    // Show the news article again
    $id = $newsRows['id'];
    $title = strip_tags(stripslashes($newsRows['title']));
    $editor1 = $show->news_article = stripslashes(html_entity_decode($newsRows['editor1']));
    $poster = $newsRows['poster'];
    $date = $newsRows['date'];

    $headColour = getColour($colour);

    echo("<h1>{$title}</h1>
        $editor1
        <i>Posted by $poster on $date</i><br>
    <hr>");


    // Get the amount of comments
    $numrows = mysql_num_rows($getCom);
    // Echo out the comments
    if(!$numrows){
        // There are no comments
        echo("<i>There are no comments for this article</i>");
    } else {
        // Show the comments
        echo("<i>There are $numrows comment(s) for this article</i><br><br>");
        while($rows = mysql_fetch_array($getCom)){
            $comment = strip_tags($rows['comment']);
            $name = $rows['name'];
            $article = $rows['article'];
                        $ip = $rows['ip'];
            $timestamp = date("m/d/y - h:ia", $rows['tstamp']);
            echo("$comment <br>Posted by $name on $timestamp <br> <br><hr>");
        }

    }

    // Show the form to submit a comment
    echo("<form method=\"post\">
        Comment: <textarea name=\"comment\" id=\"comment\" cols=\"45\" rows=\"5\"></textarea><br>
        Name:<input type=\"text\" name=\"name\"><br>
        <input type=\"hidden\" name=\"id\" value=\"{$id}\">
        <input type=\"submit\" name=\"submit\" value=\"Post comment!\"><br>
          </form>");
} else {
    // Show the main news articles
    $getNews = mysql_query("SELECT * FROM news WHERE category = 1 AND approved = 1 ORDER BY id DESC") or die(mysql_error());

    // If there are no articles
    if(mysql_num_rows($getNews) == 0){
          echo("<br /><br /><br /><div class=\"warning\"><center><b>There are currently no news articles. Please check back later!</center></b></div>");
      } else {
        // Otherwise show the articles
          while($rows = mysql_fetch_array($getNews)){
                  $id = $rows['id'];
                  $title = strip_tags(stripslashes($rows['title']));
                  $editor1 = $show->news_article = stripslashes(html_entity_decode($rows['editor1']));
                  $poster = $rows['poster'];
                  $date = $rows['date'];
     {
    $headColour = getColour($colour);
        $commentQ = mysql_query("SELECT * FROM comments_articles WHERE article='{$id}'");
        $commentR = mysql_num_rows($commentQ);
        echo("<h1>{$title}</h1>
        $editor1
        <a href=\"index.php?id=$id\">$commentR comment(s)</a> <i>Posted by $poster on $date</i><hr>");
    $colour ++;
        // If the colour count is 5 (we only have four colours), reset it back to 1
        if($colour == 5){
            $colour = 1;
        }
          } 

          }
    }
}
  ?>

提前感谢您的帮助!丹尼尔·米内特

4

0 回答 0