1

私は、到達した最大文字数に基づいて PHP がテキストを切り捨てることに精通していますが、これを文字から微調整して、テキストを切り捨てる前に 10 行に制限したいと考えています。

これを達成するにはどうすればよいでしょうか?

文字数を制限するために現在使用しているものは次のとおりです。

<?php $str = $profile['bio'];
$max = 510;
if(strlen($str) > $max) {
$str = substr($str, 0, $max) . '...'; } ?>
<?php echo $str ?> 
4

4 に答える 4

2

explode()テキストを行の配列に変換し、行array_slice()の量を制限してから、implode()すべてを再びまとめるために使用します。

<?php
    $text = "long\nline\ntext\nhere";
    $lines = explode("\n", $text);

    $lines = array_slice($lines, 0, 10); //10 is how many lines you want to keep
    $text = implode("\n", $lines);
?>
于 2013-04-16T13:09:53.643 に答える
1

最善の策は、純粋な css を使用してテキスト/コンテナーの高さを制限することだと思います。

テキストの「行」とは何ですか? フォームフィールドに書かれた純粋なテキスト? エディターからのテキストは、おそらく内部に html タグでいっぱいですか? 外国の文字を含むutf8テキスト?

「テキストの行」というフレーズに共通のパターンが見られないため、その長さ (つまり高さ) を制限するために何らかの方法を使用します。

それでもphpで制限したい場合は、長さリミッターを使用することをお勧めします. ここやウェブ上には無数の投稿があります。ただし、エンコードされたデータ (非ラテン語) には注意する必要があります。

于 2013-04-16T13:26:04.933 に答える
0

例えば

<?php
$subject = data();

$p = "![\r\n]+!";
$subject = preg_split($p, $subject, 11);
$subject = array_slice($subject, 0, 10);
echo join("\r\n", $subject);

function data() {
    return <<< eot
Mary had a little lamb,
whose fleece was white as snow.

And everywhere that Mary went,
the lamb was sure to go.

It followed her to school one day
which was against the rule.

It made the children laugh and play,
to see a lamb at school.

And so the teacher turned it out,
but still it lingered near,

And waited patiently about,
till Mary did appear.

"Why does the lamb love Mary so?"
the eager children cry.

"Why, Mary loves the lamb, you know."
 the teacher did reply.
eot;
}   

版画

Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
the lamb was sure to go.
It followed her to school one day
which was against the rule.
It made the children laugh and play,
to see a lamb at school.
And so the teacher turned it out,
but still it lingered near,
于 2013-04-16T13:11:15.250 に答える
-1

この機能を使用できます:

<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function truncateLongText ($string, $limit, $break=".", $pad="...") {
    // return with no change if string is shorter than $limit
    $string = strip_tags($string, '<b><i><u><a><s><br><strong><em>');

    if(strlen($string) <= $limit)
        return $string;
    // is $break present between $limit and the end of the string?
    if ( false !== ($breakpoint = strpos($string, $break, $limit)) ) {
        if($breakpoint < strlen($string) - 1) {
            $string = substr($string, 0, $breakpoint) . $pad;
        }
    }
    return $string;
}

使用例:

$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
echo truncateLongText($text, 10);
// Lorem Ipsum is simply dummy text of the printing and typesetting industry...
于 2013-04-16T13:09:14.523 に答える