154

MySQL データベースに説明フィールドがあり、2 つの異なるページでデータベースにアクセスします。1 つのページではフィールド全体を表示しますが、もう 1 つのページでは最初の 50 文字を表示したいだけです。説明フィールドの文字列が 50 文字未満の場合は ... は表示されませんが、そうでない場合は最初の 50 文字の後に ... が表示されます。

例 (完全な文字列):

Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters now ...

例 2 (最初の 50 文字):

Hello, this is the first example, where I am going ...
4

12 に答える 12

220

この方法でも目的のトリミングを実現できます。

mb_strimwidth("Hello World", 0, 10, "...");

どこ:

  • Hello World: トリミングする文字列。
  • 0: 文字列の先頭からの文字数。
  • 10: トリミングされた文字列の長さ。
  • ...: トリミングされた文字列の末尾に追加された文字列。

これは を返しHello W...ます。

10 は、切り捨てられた文字列 + 追加された文字列の長さであることに注意してください!

ドキュメント: http://php.net/manual/en/function.mb-strimwidth.php

単語の切り捨てを避けるには:

テキストの抜粋を提示する場合、おそらく単語の切り捨ては避けるべきです。ここで述べた以外に、切り捨てられたテキストの長さに厳しい要件がない場合はwordwrap()、次のように切り捨てて、最後の単語も切り取らないようにすることができます。

$text = "Knowledge is a natural right of every human being of which no one
has the right to deprive him or her under any pretext, except in a case where a
person does something which deprives him or her of that right. It is mere
stupidity to leave its benefits to certain individuals and teams who monopolize
these while the masses provide the facilities and pay the expenses for the
establishment of public sports.";

// we don't want new lines in our preview
$text_only_spaces = preg_replace('/\s+/', ' ', $text);

// truncates the text
$text_truncated = mb_substr($text_only_spaces, 0, mb_strpos($text_only_spaces, " ", 50));

// prevents last word truncation
$preview = trim(mb_substr($text_truncated, 0, mb_strrpos($text_truncated, " ")));

この場合、$previewになります"Knowledge is a natural right of every human being"

ライブコードの例: http://sandbox.onlinephpfunctions.com/code/25484a8b687d1f5ad93f62082b6379662a6b4713

于 2013-04-26T15:08:34.517 に答える
50

文字列が50文字より長い場合は、単語を壊さずwordwrap()に文字列を切り捨て、最後に追加するために使用します。...

$str = $input;
if( strlen( $input) > 50) {
    $str = explode( "\n", wordwrap( $input, 50));
    $str = $str[0] . '...';
}

echo $str;

そうでなければ、そうする解決策を使うことはsubstr( $input, 0, 50);言葉を壊します。

于 2012-07-11T13:50:56.803 に答える
9
<?php
function truncate($string, $length, $stopanywhere=false) {
    //truncates a string to a certain char length, stopping on a word if not specified otherwise.
    if (strlen($string) > $length) {
        //limit hit!
        $string = substr($string,0,($length -3));
        if ($stopanywhere) {
            //stop anywhere
            $string .= '...';
        } else{
            //stop on a word.
            $string = substr($string,0,strrpos($string,' ')).'...';
        }
    }
    return $string;
}
?>

上記のコードスニペットを何度も使用しています。

于 2012-07-11T13:50:26.170 に答える
2
<?php
$string = 'This is your string';

if( strlen( $string ) > 50 ) {
   $string = substr( $string, 0, 50 ) . '...';
}

それでおしまい。

于 2012-07-11T13:50:41.577 に答える
2
$string = "Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters know...";

if(strlen($string) >= 50)
{
    echo substr($string, 50); //prints everything after 50th character
    echo substr($string, 0, 50); //prints everything before 50th character
}
于 2012-07-11T13:51:35.483 に答える
2
// this method will return the break string without breaking word
$string = "A brown fox jump over the lazy dog";
$len_required= 10;
// user strip_tags($string) if string contain html character
if(strlen($string) > 10)
{
 $break_str = explode( "\n", wordwrap( $string , $len_required));
 $new_str =$break_str[0] . '...';
}
// other method is use substr 
于 2019-09-13T07:01:20.400 に答える
1

str_split()これに使用できます

$str = "Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters know...";
$split = str_split($str, 50);
$final = $split[0] . "...";
echo $final;
于 2012-07-11T13:53:16.757 に答える