减少从数据库中显示的文本

时间:2015-02-06 08:36:09

标签: php mysql sql database

在第一页上,我想将$row['opis']限制为100个字符,但用户可以输入500个字符,因此我需要在第一页中限制$row['opis']。我怎么能这样做?

这是我的代码:

<?php
                            mysql_connect("localhost","root","123") or die(mysql_error());
                            mysql_select_db("database") or die(mysql_error());
                            $strSQL = "SELECT * FROM table_name ORDER BY id DESC";
                            $rs = mysql_query($strSQL);

                            while($row = mysql_fetch_array($rs)) {

                        echo '<div class="col-md-3 col-sm-6 masonry-grid-item">
                                    <div class="listing-item">
                                    <div class="overlay-container">
                                        <img src="images/product-1.png" alt="">
                                        <a href="oglas/index.php?id=' . $row['id'] . '" class="overlay small">
                                            <i class="fa fa-plus"></i>
                                            <span>View Details</span>
                                        </a>
                                    </div>
                                    <div class="listing-item-body clearfix">
                                        <h3 class="title"><a href="oglas/index.php?id=' . $row['id'] . '">'. $row['naziv'] .'</a></h3>
                                        <p>' . $row['opis'] . '</p>
                                        <span class="price">' . $row['tel'] . '</span>
                                        <div class="elements-list pull-right">
                                            <a href="#" class="wishlist" title="wishlist"><i class="fa fa-heart-o"></i></a>
                                            <a href="oglas/index.php?id=' . $row['id'] . '">Pogledaj</a>
                                        </div>
                                    </div>
                                </div>
                            </div>';

                            }
                            mysql_close();
                        ?>      

谢谢!

4 个答案:

答案 0 :(得分:1)

您可以使用mb_strimwidth来显示前100个字符

<?php
                            mysql_connect("localhost","root","123") or die(mysql_error());
                            mysql_select_db("database") or die(mysql_error());
                            $strSQL = "SELECT * FROM table_name ORDER BY id DESC";
                            $rs = mysql_query($strSQL);

                            while($row = mysql_fetch_array($rs)) {

                        echo '<div class="col-md-3 col-sm-6 masonry-grid-item">
                                    <div class="listing-item">
                                    <div class="overlay-container">
                                        <img src="images/product-1.png" alt="">
                                        <a href="oglas/index.php?id=' . $row['id'] . '" class="overlay small">
                                            <i class="fa fa-plus"></i>
                                            <span>View Details</span>
                                        </a>
                                    </div>
                                    <div class="listing-item-body clearfix">
                                        <h3 class="title"><a href="oglas/index.php?id=' . $row['id'] . '">'. $row['naziv'] .'</a></h3>
                                        <p>' . mb_strimwidth($row["opis"], 0, 100, "") . '</p>
                                        <span class="price">' . $row['tel'] . '</span>
                                        <div class="elements-list pull-right">
                                            <a href="#" class="wishlist" title="wishlist"><i class="fa fa-heart-o"></i></a>
                                            <a href="oglas/index.php?id=' . $row['id'] . '">Pogledaj</a>
                                        </div>
                                    </div>
                                </div>
                            </div>';

                            }
                            mysql_close();
                        ?>

<强>解释

mb_strimwidth($YOUR_STRING, 0, 100, "")

0表示修剪从第一个字符开始,100是字符的限制。第四个选项允许您在新字符串的末尾添加类似“...”的内容。

修改

您也可以使用substr,但它使用的是字节数,而不是字符数。因此,如果您使用的是多字节编码,如UTF-8(希腊语,塞尔维亚语,罗马尼亚语等),则应使用上述mb_strimwidth

答案 1 :(得分:1)

NETCreator非常感谢!

答案是添加mb_strimwidth($ YOUR_STRING,0,100,“”)......

<?php
                        mysql_connect("localhost","root","123") or die(mysql_error());
                        mysql_select_db("database") or die(mysql_error());
                        $strSQL = "SELECT * FROM table_name ORDER BY id DESC";
                        $rs = mysql_query($strSQL);

                        while($row = mysql_fetch_array($rs)) {

                    echo '<div class="col-md-3 col-sm-6 masonry-grid-item">
                                <div class="listing-item">
                                <div class="overlay-container">
                                    <img src="images/product-1.png" alt="">
                                    <a href="oglas/index.php?id=' . $row['id'] . '" class="overlay small">
                                        <i class="fa fa-plus"></i>
                                        <span>View Details</span>
                                    </a>
                                </div>
                                <div class="listing-item-body clearfix">
                                    <h3 class="title"><a href="oglas/index.php?id=' . $row['id'] . '">'. $row['naziv'] .'</a></h3>
                                    <p>' . mb_strimwidth($row["opis"], 0, 100, "") . '</p>
                                    <span class="price">' . $row['tel'] . '</span>
                                    <div class="elements-list pull-right">
                                        <a href="#" class="wishlist" title="wishlist"><i class="fa fa-heart-o"></i></a>
                                        <a href="oglas/index.php?id=' . $row['id'] . '">Pogledaj</a>
                                    </div>
                                </div>
                            </div>
                        </div>';

                        }
                        mysql_close();
                    ?>

答案 2 :(得分:1)

你也可以使用substr ..下面的代码显示前4个字符。

echo substr('abcdef', 0, 4);  

输出 abcd

答案 3 :(得分:0)

@Mile你可以这样做

$text = substr($text,$start,$length);

此处$text是您的字符串,$start是您的起点,$length是您想要显示字符串的时间。