我希望将html电子邮件中150px宽度的表格的文本限制为固定行数,例如:
Long text continues down the road into a lane and doesn't stop there
我希望它看起来像这样:
Long text continues down
the road into a lane and...
我将字符串截断为最多45个字符(包括省略号),但有时当出现一个长词时,它会变成三行:
Long text continues at
accelerating speed into the
road...
理想情况下,我想打破单词加速,或者更确切地说,在第一行中填充尽可能多的字符,并继续到第二行,有没有办法在html中做到这一点?(我查看了自动换行功能,但显然并不是所有电子邮件客户端都支持它)
此外,由于这是电子邮件客户端,我不能做任何javascript等。
发布于 2011-08-30 05:24:11
CSS解决方案
你可以设置一个高度,并做溢出隐藏。
span {
display: inline-block;
border: black 1px solid;
width: 300px;
height: 40px;
overflow: hidden;
}
示例:http://jsfiddle.net/imoda/REs2Q/
PHP解决方案
服务器端的替代方案是使用substr
<?php
$string = "Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision. But when I try to look at you, you scurry away. Are you shy, squiggly line? Why only when I ignore you, do you return to the center of my eye? Oh, squiggly line, it's alright, you are forgiven.";
echo charlimit($string, 100);
function charlimit($string, $limit) {
return substr($string, 0, $limit) . (strlen($string) > $limit ? "..." : '');
}
?>
示例:http://codepad.org/OetkaMh6
这将输出100个字符的字符串,然后将...
技巧附加到此,您必须将字符数更改为最适合您的情况。因为它是服务器端的,所以它不知道在每个场景中需要多少个字符才能仅触发一个回车。
或者,您可以限制字数:
<?php
$string = "Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision. But when I try to look at you, you scurry away. Are you shy, squiggly line? Why only when I ignore you, do you return to the center of my eye? Oh, squiggly line, it's alright, you are forgiven.";
echo wordlimit($string, 20);
function wordlimit($string, $limit) {
$overflow = true;
$array = explode(" ", $string);
$output = '';
for ($i = 0; $i < $limit; $i++) {
if (isset($array[$i])) $output .= $array[$i] . " ";
else $overflow = false;
}
return trim($output) . ($overflow ? "..." : '');
}
?>
示例:http://codepad.org/WYJFPaD5
但这是一回事,你必须把它裁剪成“最合适的”
希望这能有所帮助。
发布于 2011-08-30 06:17:42
如果您的消息是字符串,则可以使用PHP执行以下操作:
$stringChunkArray = str_split($string, 45); // 45 = desired char count
foreach ($stringChunkArray as $line) {
echo $line.PHP_EOL;
}
这将保证每行45个字符...
发布于 2011-08-30 05:10:13
您是否尝试过在<span style="white-space: nobreak;">...</span>
中对每一行进行包装,以强制其不进行包装?
一些文档可以在here中找到
https://stackoverflow.com/questions/7238926
复制