【问题标题】:Limit Displayed Character PHP限制显示字符 PHP
【发布时间】:2014-01-03 06:09:37
【问题描述】:

我在这里有点困惑。我正在创建一个博客,我想限制一篇文章的字符数,以便当读者点击“阅读更多”或“...”时,他们能够阅读整篇文章。我正在搜索并尝试理解代码,但我对将代码放在哪里感到很困惑。我找到了这段代码

// strip tags to avoid breaking any html
$string = strip_tags($string);
if (strlen($string) > 500) {
// truncate string
$stringCut = substr($string, 0, 500);
// make sure it ends in a word so assassinate doesn't become ass...
$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>'; 
}
echo $string;

来自limit text length in php and provide 'Read more' link。这是我的代码

$articlesql = "SELECT articles.*, categories.category_name, admin.admin_name FROM articles, categories, admin WHERE articles.article_category_id=categories.category_id AND articles.article_admin_id=admin.admin_id";
$articleresult = mysql_query($articlesql);
while($articlerow=mysql_fetch_assoc($articleresult))
echo"
<li>
    <span></span>
    <div>
        <h4><a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>".$articlerow['article_title']."</a></h4>
        <span>
            Posted in <a href = '#'>".$articlerow['category_name']."</a> by <a href='#'>".$articlerow['admin_name']."</a> on <a href='#'>".$articlerow['article_date']."</a>
        </span>
    </div>
    <a href='article-single.php?id=".$articlerow['article_id']."' title='view details'><img src='images/blog-post-1.jpg' alt=''></a>
    <p>".$articlerow['article_content']."</p>
</li>
";

我真的需要有人帮助我。谢谢!

【问题讨论】:

标签: php limit


【解决方案1】:

试试

<?php
$articlesql = "SELECT articles.*, categories.category_name, admin.admin_name FROM articles, categories,
                admin WHERE articles.article_category_id=categories.category_id AND articles.article_admin_id=admin.admin_id";
$articleresult = mysql_query($articlesql);
while($articlerow=mysql_fetch_assoc($articleresult)){

    $string = strip_tags($articlerow['article_content']);
    if (strlen($string) > 500) {
    // truncate string
    $stringCut = substr($string, 0, 500);
    // make sure it ends in a word so assassinate doesn't become ass...
    $string = substr($stringCut, 0, strrpos($stringCut, ' '))."... <a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>Read More</a>";
    }

    echo "
        <li>
            <span></span>
            <div>
                <h4><a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>".$articlerow['article_title']."</a></h4>
                <span>
                    Posted in <a href = '#'>".$articlerow['category_name']."</a> by <a href='#'>".$articlerow['admin_name']."</a> on <a href='#'>".$articlerow['article_date']."</a>
                </span>
            </div>
            <a href='article-single.php?id=".$articlerow['article_id']."' title='view details'><img src='images/blog-post-1.jpg' alt=''></a>
            <p>".$string."</p>
        </li>
    ";
}

【讨论】:

    【解决方案2】:

    你只需要 PHP 的 strrpos 和 substr 函数

    编写以下查询:

    $excerpt_query = mysql_fetch_array(mysql_query("SELECT LEFT(article_content, 30) as excerpt FROM article")) or die(mysql_error());

    现在您将在输出的摘录列中包含前 30 个字符。您可以使用每个文章 ID 愉快地玩弄它。

    $excerpt = $excerpt_query['excerpt'];
    $spaceIndex = strrpos($excerpt, ' '); //Finds the last space from the excerpt value.
    

    现在echo substr($excerpt, 0, $spaceIndex); // this will echo the correct string as the excerpt.

    这应该是实际的阅读更多链接:

    echo substr($excerpt, 0, $spaceIndex) . '<a href="single.php?id=1">Read More...</a>';
    

    如果您有任何疑问,请加入。

    【讨论】:

      【解决方案3】:

      我几乎不知道您的代码中发生了什么,但您只需要限制查询中的文本 例如这将得到 100 个第一个单词

      LEFT(article, 100) AS first100
      

      当你想显示文章时(在一个while循环中)你应该输入'...'而不破坏一个单词

      $extract = $articlerow['first100'];
      $lastSpace = strrpos($extract, ' ');
      echo substr($extract, 0, $lastSpace) . '... ';
      

      【讨论】:

        【解决方案4】:

        我建议使用Truncate text containing HTML, ignoring tags 中的 printTruncated 函数。

        使用上面strip_tags的简单代码,设置substr,就不会显示有限制的格式化文章内容了。

        按如下方式使用 printTruncated 函数,将显示格式化的内容。

        printTruncated(500,$articlerow['article_content'])
        

        在上述函数的最后,你可以设置阅读更多链接,你可以创建一个JavaScript,通过隐藏截断的内容和显示完整的内容来显示完整的内容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-26
          • 1970-01-01
          • 2011-12-10
          • 1970-01-01
          • 1970-01-01
          • 2016-02-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多