将文本限制为只有两行 [英] limiting text to only two lines

查看:141
本文介绍了将文本限制为只有两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML电子邮件中将文本限制为150像素宽度的表格的固定行数,例如:

我想让它看起来像这样:

 长文本继续沿着
路进入车道和...

字符串,最多包含45个字符,包括省略号,但有时候当出现长字时,它会变成三行:

 长文本继续在
加速到
路...


$ b b

理想情况下,我想打破单词加速或填充许多字符,它可以在第一行,继续第二行,有没有办法在html中做这个? (我查看了单词,但显然不是所有的电子邮件客户端都支持)



也因为这是电子邮件客户端,我不能做任何javascript等。

您可以设置一个高度和do overflow hidden。

  span {
display:inline-block;
border:black 1px solid;
width:300px;
height:40px;
overflow:hidden;
}

示例http://jsfiddle.net/imoda/REs2Q/






PHP解决方案



服务器端的选择是使用 substr

 <?php 

$ string =哦,我的眼睛流畅的扭曲的线条。但是当我试着看你的时候,你就匆忙离开了,你害羞,弯曲的线条?为什么只有当我忽略你,你回到我的眼睛的中心吗?哦,弯曲的线,好了,你被原谅。;

echo charlimit($ string,100);

函数charlimit($ string,$ limit){

返回substr($ string,0,$ limit)。 (strlen($ string)> $ limit?...:'');
}

?>

http://codepad.org/OetkaMh6



这将输出字符串的100个字符,然后附加 .. 。使用这个技巧,你必须将字符数改为最适合你的情况。因为它是服务器端的,它不知道在每个场景中需要多少字符才能触发一个回车。



或者,您可以限制字数:

 <?php 

$ string =哦,我的眼液中有扭曲的线你潜伏在我的视线的外围,但当我试图看着你,你匆忙离开。你害羞,弯曲线?为什么只有当我忽略你,你回到我的眼睛的中心吗?哦,波纹线,这是好的,你是原谅。

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



但这是一回事,你必须将其定制为最适合 p>

希望有帮助。


I want to limit the text to a fixed number of lines for a table of 150px width in html email, for example:

Long text continues down the road into a lane and doesn't stop there

I want it to look like this:

Long text continues down 
the road into a lane and...

I am truncating the string for a max of 45 characters including ellipsis, but sometimes when a long word is present it goes to three lines:

Long text continues at
accelerating speed into the
road...

Ideally I would like to break the word accelerating or rather fill as many characters it can in the first line and continue to second line, is there a way to do this in html? (I looked at the word-wrap but apparently it is not supported in all email clients)

Also since this is email clients, I can't do any javascript etc. also.

解决方案

CSS Solution

You could set a height and do overflow hidden.

span {
    display: inline-block;
    border: black 1px solid;
    width: 300px;
    height: 40px;
    overflow: hidden;
}

Example: http://jsfiddle.net/imoda/REs2Q/


PHP Solution

The server sided alternative is to use 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 ? "..." : '');
    }

?>

Example: http://codepad.org/OetkaMh6

This will output 100 characters of the string then append ... Trick with this is you'll have to change the number of characters to what works best for your situation. Because it's server sided, it won't know how many characters it takes in each scenario to trigger only one carriage return.

Alternatively, you can limit the number of words:

<?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 ? "..." : '');
    }

?>

Example: http://codepad.org/WYJFPaD5

But it's the same thing, you have to tailor it to "best fit"

Hope that helps.

这篇关于将文本限制为只有两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆