简体   繁体   English

PHP - 如何在某一点将“...”附加到不同长度的字符串? 例如,200个字符之后

[英]PHP - How to append “…” to a string of varying length at a certain point? e.g. After 200 chars

i have a mysql database and a table full of information, specifically a column of product descriptions. 我有一个mysql数据库和一个充满信息的表,特别是一列产品描述。 This column is used in my scripts twice. 这个列在我的脚本中使用了两次。 Its max length is 1000 characters. 最大长度为1000个字符。

One of the uses for this information is a brief description of any given product. 此信息的用途之一是对任何给定产品的简要描述。 I would like to display a shortened version of the description that I have just mentioned, so it will be say for instance 200 characters long. 我想显示我刚刚提到的描述的缩短版本,所以例如可以说200个字符。

Now, using PHP I would like to append the string with "..." (3 dots) at the point where it reaches 200 characters. 现在,使用PHP我想在其达到200个字符的位置附加“...”(3个点)的字符串。 So basically only the first 200 characters are displayed. 所以基本上只显示前200个字符。

How would I go about something like this? 我怎么会这样做? I am sure it will be a fairly simple task but the internet is a big place to look for something you don't know the name of :) 我相信这将是一个相当简单的任务,但互联网是一个寻找你不知道名称的东西的大地方:)

If anyone could help in this regard, with an example or some links relating to the relevant functions that would be great. 如果有人可以在这方面提供帮助,可以通过示例或与相关功能相关的一些链接来实现。 Thanks a lot! 非常感谢!

您可以使用substr实现此目的。

$string = substr($string,0,197) . "...";

This one takes care of strings that aren't actually 200 chars yet. 这个用来处理实际上不是200个字符的字符串。

$string = 'your long string goes here';
if (strlen($string) >= 200) {
    $string = substr($string, 0, 197).'...';
}

Source: php.net/substr 资料来源: php.net/substr

May I give you an alternative made in pure CSS (good for headlines, for example)? 我可以给你一个纯CSS的替代品(例如,适用于头条新闻)吗?

overflow: hidden; white-space: nowrap; text-overflow: ellipsis;

cuts off too long text automatically if the element around it is not long / big enough. 如果周围的元素不够长/大,则会自动切断太长的文本。

$string = 'asd';
$maxLength = 200;
if (mb_strlen($string) > $maxLength) {
    $string = mb_substr($string, 0, $maxLength) . "...";
}

Only collect 200 characters from your database in the first place. 首先只从您的数据库中收集200个字符。

$sql = "SELECT LEFT(article, 200) as article WHERE ETC ETC "

Then apply something like this , but essentially you need to go back to the previous word end chop off the rest and apend an elipsis. 然后应用这样的东西 ,但基本上你需要回到前一个单词结束砍掉其余部分并重新进行省略。

…

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串中的一系列连续字符如何在特定点将其拆分,例如php中的30个字符 - A continues series of characters in a string how do I split it at a certain point e.g 30 charaacters in php 如何在PHP中循环$ num,例如1、2、3、4、1、2、3、4、1、2、3、4 - How to loop $num in php, e.g. 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 如果未知变量(例如电子邮件)在字符串中,如何在 PHP8 中检查? - How to check in PHP8 if a unknown variable (e.g. e-mail) is in a string? 如何在特定时间间隔内运行PHP脚本(例如每天一次)? - How can I run PHP script in certain interval (e.g. once a day)? 如何将PHP时区字符串(例如欧洲/柏林)转换为codeigniter时区(例如UP1) - how to convert PHP timezone string(e.g Europe/Berlin) to codeigniter timezone(e.g. UP1) PHP Regexp捕获重复的字符组,例如hahaha jajajaja hihihi - PHP Regexp capturing repeating group of chars, e.g. hahaha jajajaja hihihi 将从文件读取的字符串更改为特殊字符(例如“ \\ n”) - Change string read from file into special chars (e.g. “\n”) php - regex - 如何从字符串中提取带小数(点和逗号)的数字(例如1,120.01)? - php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)? 如何提取某些HTML标签,例如 <ul> 在PHP中使用带有preg_match_all的Regex? - How can I extract certain HTML tags e.g. <ul> using Regex with preg_match_all in PHP? PHP中的“ @”符号,例如“ @ $ name [1]”吗? - '@' symbol in php, e.g. '@$name[1]'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM