PHPの通常のsubstr()
関数を使用すると、文字列のカットを「開始」する場所を決定したり、長さを設定したりすることができます。長さはおそらく最も使用されますが、この場合、最初から約 120 文字を切り取る必要があります。問題は、文字列内の html をそのまま維持し、タグ内の実際のテキストのみを切り取る必要があることです。
いくつかのカスタム関数を見つけましたが、開始点を設定できる関数は 1 つも見つかりませんでした。弦を切り始めたいところ。
ここに私が見つけたものがあります:フォーマットを保持し、HTMLを壊さずにPHP substr()とstrip_tags()を使用する
したがって、基本的には、substr()
フォーマットを維持することを除いて、元の関数とまったく同じように機能する関数が必要です。
助言がありますか?
変更するコンテンツの例:
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going <a href="#">through the cites</a> of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus</p> <p>Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the <strong>Renaissance</strong>. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
最初から5をカットした後:
<p>ary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going <a href="#">through the cites</a> of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus</p> <p>Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the <strong>Renaissance</strong>. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
そして、最初と最後から5つ:
<p>ary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going <a href="#">through the cites</a> of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus</p> <p>Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the <strong>Renaissance</strong>. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.1</p>
ええ、あなたは私のドリフトをキャッチしますか?
単語の途中で切り捨てるのであれば単語全体を切り捨てた方がいいと思いますが、それほど重要ではありません。
** 編集: ** 固定引用符。