Text
class Cake\View\Helper\TextHelper(View $view, array $config = [])
TextHelper には、ビュー内のテキストをより使いやすく見やすくするためのメソッドが含まれています。 リンクの有効化、URLのフォーマット、選択した単語やフレーズの周囲のテキストの抜粋を作成、 テキストブロック内のキーワードのハイライト、 長いテキストの綺麗な切り詰めなどを支援します。
メールアドレスのリンク化
method Cake\View\Helper\TextHelper::autoLinkEmails(string $text, array $options = [])
$options で定義されたオプションに従い、 $text に整形されたメールアドレスのリンクを追加します。( HtmlHelper::link() を参照) :
$myText = 'For more information regarding our world-famous ' .
'pastries and desserts, contact [email protected]';
$linkedText = $this->Text->autoLinkEmails($myText);出力:
For more information regarding our world-famous pastries and desserts,
contact <a href="mailto:[email protected]">[email protected]</a>このメソッドは自動的に入力をエスケープします。これを無効にする必要がある場合には、 escape オプションを使用します。
URLのリンク化
method Cake\View\Helper\TextHelper::autoLinkUrls(string $text, array $options = [])
autoLinkEmails() と同様ですが、https, http, ftp, nntp から始まる文字列を見つけ、適切にリンクします。
このメソッドは自動的に入力をエスケープします。これを無効にする必要がある場合には、 escape オプションを使用します。
URLとメールアドレス両方のリンク化
method Cake\View\Helper\TextHelper::autoLink(string $text, array $options = [])
与えられた $text に対して、 autoLinkUrls() と autoLinkEmails() 両方の機能を実行します。 全てのURLとメールアドレスは与えられた $options を考慮して適切にリンクされます。
このメソッドは自動的に入力をエスケープします。これを無効にする必要がある場合には、 escape オプションを使用します。
テキストの段落化
method Cake\View\Helper\TextHelper::autoParagraph(string $text)
2行改行されている場合は適切にテキストを<p>で囲み、 1行改行には<br>を追加します。:
$myText = 'For more information
regarding our world-famous pastries and desserts.
contact [email protected]';
$formattedText = $this->Text->autoParagraph($myText);出力:
<p>For more information<br />
regarding our world-famous pastries and desserts.</p>
<p>contact [email protected]</p>文字列の一部をハイライトする
method Cake\Utility\Text::highlight(string $haystack, string $needle, array $options = [] )
$options['format'] で指定された文字列か、デフォルトの文字列を使って $haystack 中の $needle をハイライトします。
オプション:
formatstring - ハイライトするフレーズに適用する HTML パーツhtmlbool -trueならすべての HTML タグを無視して、正確にテキストのみをハイライトするよう保証します。
例:
// TextHelper として呼ぶ
echo $this->Text->highlight(
$lastSentence,
'使って',
['format' => '<span class="highlight">\1</span>']
);
// Text として呼ぶ
use Cake\Utility\Text;
echo Text::highlight(
$lastSentence,
'使って',
['format' => '<span class="highlight">\1</span>']
);出力:
$options['format'] で指定された文字列か、デフォルトの文字列を<span class="highlight">使って</span>
$haystack 中の $needle をハイライトします。リンク除去
method Cake\Utility\Text::stripLinks($text)
渡された $text から HTML リンクを取り除きます。
テキストの切り詰め
method Cake\Utility\Text::truncate(string $text, int $length = 100, array $options)
$text が $length より長い場合、このメソッドはそれを $length の長さに切り詰め、 'ellipsis' が定義されているなら末尾にその文字列を追加します。 もし 'exact' に false が渡されたなら、 $length を超えた最初の空白で切り詰められます。 もし 'html' に true が渡されたなら、HTML タグは尊重され、削除されなくなります。
$options はすべての追加パラメーターを渡すのに使われ、下記のようなキーがデフォルトになっており、すべてが省略可能です。 :
[
'ellipsis' => '...',
'exact' => true,
'html' => false
]例:
// TextHelper として呼ぶ
echo $this->Text->truncate(
'The killer crept forward and tripped on the rug.',
22,
[
'ellipsis' => '...',
'exact' => false
]
);
// Text として呼ぶ
use Cake\Utility\Text;
echo Text::truncate(
'The killer crept forward and tripped on the rug.',
22,
[
'ellipsis' => '...',
'exact' => false
]
);出力:
The killer crept...
文字列の末尾を切り詰める
method Cake\Utility\Text::tail(string $text, int $length = 100, array $options)
$text が $length より長い場合、このメソッドは先頭から差となる長さの文字列を取り除き、 'ellipsis' が定義されているなら先頭にその文字列を追加します。 もし 'exact' に false が渡されたなら、切り詰めが本来発生したであろう場所の前にある最初の空白で切り詰められます。
$options はすべての追加パラメーターを渡すのに使われ、下記のようなキーがデフォルトになっており、すべてが省略可能です。 :
[
'ellipsis' => '...',
'exact' => true
]例:
$sampleText = 'I packed my bag and in it I put a PSP, a PS3, a TV, ' .
'a C# program that can divide by zero, death metal t-shirts'
// TextHelper として呼ぶ
echo $this->Text->tail(
$sampleText,
70,
[
'ellipsis' => '...',
'exact' => false
]
);
// Text として呼ぶ
use Cake\Utility\Text;
echo Text::tail(
$sampleText,
70,
[
'ellipsis' => '...',
'exact' => false
]
);出力:
...a TV, a C# program that can divide by zero, death metal t-shirts
抜粋の抽出
method Cake\Utility\Text::excerpt(string $haystack, string $needle, integer $radius=100, string $ellipsis="...")
$haystack から、 $needle の前後 $radius で指定された文字数分を含む文字列を抜粋として抽出し、 その先頭と末尾に $ellipsis の文字列を追加します。 このメソッドは検索結果には特に便利でしょう。クエリー文字列やキーワードを結果の文章中とともに表示することができます。 :
// TextHelper として呼ぶ
echo $this->Text->excerpt($lastParagraph, 'method', 50, '...');
// Text として呼ぶ
use Cake\Utility\Text;
echo Text::excerpt($lastParagraph, 'method', 50, '...');出力:
... by $radius, and prefix/suffix with $ellipsis. This method is especially
handy for search results. The query...
配列を文章的なものに変換する
method Cake\Utility\Text::toList(array $list, $and='and', $separator=', ')
最後の2要素が 'and' で繋がっている、カンマ区切りのリストを生成します。 :
$colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// TextHelper として呼ぶ
echo $this->Text->toList($colors);
// Text として呼ぶ
use Cake\Utility\Text;
echo Text::toList($colors);出力:
red, orange, yellow, green, blue, indigo and violet