Php 回显部分文本

Php 回显部分文本,php,Php,我只想显示段落的两行。 我该怎么做 <p><?php if($display){ echo $crow->content;} ?></p> 根据您所指的文本内容,您可能可以避免以下情况: // `nl2br` is a function that converts new lines into the '<br/>' element. $newContent = nl2br($crow->content); // `explode

我只想显示段落的两行。
我该怎么做

<p><?php if($display){ echo $crow->content;} ?></p>


根据您所指的文本内容,您可能可以避免以下情况:

// `nl2br` is a function that converts new lines into the '<br/>' element.
$newContent = nl2br($crow->content);

// `explode` will then split the content at each appearance of '<br/>'.
$splitContent = explode("<br/>",$newContent);

// Here we simply extract the first and second items in our array.
$firstLine = $splitContent[0];
$secondLine = $splitContent[1];
/`nl2br`是一个将新行转换为'
'元素的函数。 $newContent=nl2br($crow->content); //“explode”将在“
”的每个外观处拆分内容。 $splitContent=explode(“
,$newContent”); //这里我们只提取数组中的第一项和第二项。 $firstLine=$splitContent[0]; $secondLine=$splitContent[1];

注意-这将破坏文本中的所有换行符!如果仍要将文本保留为原始格式,则必须再次插入它们。

根据您所指的文本内容,您可能可以避免以下情况:

// `nl2br` is a function that converts new lines into the '<br/>' element.
$newContent = nl2br($crow->content);

// `explode` will then split the content at each appearance of '<br/>'.
$splitContent = explode("<br/>",$newContent);

// Here we simply extract the first and second items in our array.
$firstLine = $splitContent[0];
$secondLine = $splitContent[1];
/`nl2br`是一个将新行转换为'
'元素的函数。 $newContent=nl2br($crow->content); //“explode”将在“
”的每个外观处拆分内容。 $splitContent=explode(“
,$newContent”); //这里我们只提取数组中的第一项和第二项。 $firstLine=$splitContent[0]; $secondLine=$splitContent[1];

注意-这将破坏文本中的所有换行符!如果仍要将文本保留为原始格式,则必须再次插入它们。

如果您指的是句子,则可以通过分解段落并选择数组的前两部分来执行此操作:

$array = explode('.', $paragraph);
$2lines = $array[0].$array[1];
否则,必须计算两行中的字符数并使用substr()函数。例如,如果两行的长度为100个字符,则应执行以下操作:

$2lines = substr($paragraph, 0, 200);
但是,由于并非所有字体字符的宽度都相同,因此可能很难准确地做到这一点。我建议使用最宽的字符,例如“W”和“echo”,将这些字符中的许多字符放在一行中。然后计算可以跨两行显示的最大字符的最大数目。从这一点,你将有一个最佳的数字。虽然这不会给你一个紧凑的两行,它将确保它不能超过两行

然而,这可能导致一个词被一分为二。为了解决这个问题,我们可以使用explode函数在提取的字符中找到最后一个单词

$array = explode(' ', $2lines);
然后,我们可以找到最后一个单词,并从最终输出中删除正确数量的字符

$numwords = count($array);
$lastword = $array[$numwords];
$numchars = strlen($lastword);
$2lines = substr($2lines, 0, (0-$numchars));

如果你指的是句子,你可以通过分解段落并选择数组的前两部分来完成:

$array = explode('.', $paragraph);
$2lines = $array[0].$array[1];
否则,必须计算两行中的字符数并使用substr()函数。例如,如果两行的长度为100个字符,则应执行以下操作:

$2lines = substr($paragraph, 0, 200);
但是,由于并非所有字体字符的宽度都相同,因此可能很难准确地做到这一点。我建议使用最宽的字符,例如“W”和“echo”,将这些字符中的许多字符放在一行中。然后计算可以跨两行显示的最大字符的最大数目。从这一点,你将有一个最佳的数字。虽然这不会给你一个紧凑的两行,它将确保它不能超过两行

然而,这可能导致一个词被一分为二。为了解决这个问题,我们可以使用explode函数在提取的字符中找到最后一个单词

$array = explode(' ', $2lines);
然后,我们可以找到最后一个单词,并从最终输出中删除正确数量的字符

$numwords = count($array);
$lastword = $array[$numwords];
$numchars = strlen($lastword);
$2lines = substr($2lines, 0, (0-$numchars));

这是一个更一般的答案-您可以使用以下方法获得任意数量的行:

function getLines($paragraph, $lines){
    $lineArr = explode("\n",$paragraph);
    $newParagraph = null;
    if(count($lineArr) > 0){
        for($i = 0; $i < $lines; $i++){
            if(isset($lines[$i]))
                $newParagraph .= $lines[$i];
            else
                break;
        }
    }
    return $newParagraph;
}
函数getLines($段落,$lines){
$lineArr=分解(“\n”,$段落);
$newparagration=null;
如果(计数($lineArr)>0){
对于($i=0;$i<$line;$i++){
如果(isset($line[$i]))
$newparagration.=$lines[$i];
其他的
打破
}
}
返回$new段落;
}

您可以使用echogetlines($crow->content,2)来做你想做的事。

这是一个更一般的答案-你可以使用以下方法获得任意数量的行:

function getLines($paragraph, $lines){
    $lineArr = explode("\n",$paragraph);
    $newParagraph = null;
    if(count($lineArr) > 0){
        for($i = 0; $i < $lines; $i++){
            if(isset($lines[$i]))
                $newParagraph .= $lines[$i];
            else
                break;
        }
    }
    return $newParagraph;
}
function getLines($text, $lines)
{
    $text = explode("\n", $text, $lines + 1); //The last entrie will be all lines you dont want.
    array_pop($text); //Remove the lines you didn't want.

    return implode("<br>", $text); //Implode with "<br>" to a string. (This is for a HTML page, right?)
}

echo getLines($crow->content, 2); //The first two lines of $crow->content
函数getLines($段落,$lines){
$lineArr=分解(“\n”,$段落);
$newparagration=null;
如果(计数($lineArr)>0){
对于($i=0;$i<$line;$i++){
如果(isset($line[$i]))
$newparagration.=$lines[$i];
其他的
打破
}
}
返回$new段落;
}
您可以使用echogetlines($crow->content,2)来做你想做的。

函数getLines($text,$lines)
function getLines($text, $lines)
{
    $text = explode("\n", $text, $lines + 1); //The last entrie will be all lines you dont want.
    array_pop($text); //Remove the lines you didn't want.

    return implode("<br>", $text); //Implode with "<br>" to a string. (This is for a HTML page, right?)
}

echo getLines($crow->content, 2); //The first two lines of $crow->content
{ $text=explode(“\n”,$text,$lines+1);//最后一个entrie将是您不需要的所有行。 array_pop($text);//删除不需要的行。 返回内爆(“
”,$text);//使用“
”内爆为字符串。(这是用于HTML页面的,对吗?) } echo getLines($crow->content,2)//$crow->content的前两行
函数getLines($text,$lines)
{
$text=explode(“\n”,$text,$lines+1);//最后一个entrie将是您不需要的所有行。
array_pop($text);//删除不需要的行。
返回内爆(“
”,$text);//使用“
”内爆为字符串。(这是用于HTML页面的,对吗?) } echo getLines($crow->content,2)//$crow->content的前两行
试试这个:

$lines = preg_split("/[\r\n]+/", $crow->content, 3);
echo $lines[0] . '<br />' . $lines[1];
$lines=preg\u split(“/[\r\n]+/”,$crow->content,3);
echo$行[0]。'
$行[1];
对于可变行数,请使用:

$num_of_lines = 2;
$lines = preg_split("/[\r\n]+/", $crow->content, $num_of_lines+1);
array_pop($lines);
echo implode('<br />', $lines);
$num_of_行=2;
$lines=preg_split(“/[\r\n]+/”,$crow->content,$num_of_line+1);
数组_pop($行);
回波内爆(“
”,$lines);
干杯

试试这个:

$lines = preg_split("/[\r\n]+/", $crow->content, 3);
echo $lines[0] . '<br />' . $lines[1];
$lines=preg\u split(“/[\r\n]+/”,$crow->content,3);
echo$行[0]。'
$行[1];
对于可变行数,请使用:

$num_of_lines = 2;
$lines = preg_split("/[\r\n]+/", $crow->content, $num_of_lines+1);
array_pop($lines);
echo implode('<br />', $lines);
$num_of_行=2;
$lines=preg_split(“/[\r\n]+/”,$crow->content,$num_of_line+1);
数组_pop($行);
回声内爆('
',$li