在第一页上,我想将$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();
?>
谢谢!
答案 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
是您想要显示字符串的时间。