String クラスは文字列の作成や走査に関する便利なクラスです。 また、スタティックにアクセスすることが可能です。例: String::uuid() 。
もし、View 以外で TextHelper が必要な場合、 String クラスを使ってください。
class UsersController extends AppController {
public $components = array('Auth');
public function afterLogin() {
App::uses('String', 'Utility');
$message = $this->User->find('new_message');
if (!empty($message)) {
// 新しいメッセージをユーザへ通知
$this->Session->setFlash(__('You have a new message: %s', String::truncate($message['Message']['body'], 255, array('html' => true))));
}
}
}
バージョン 2.1 で変更: TextHelper のいくつかのメソッドは String クラスへ移動しています。
uuid メソッドは、RFC 4122 で規定されているようなユニークIDを生成するために利用します。 uuid とは、485fc381-e790-47a3-9794-1337c0a8fe68 のようなフォーマットの128ビットの文字列のことです。
String::uuid(); // 485fc381-e790-47a3-9794-1337c0a8fe68
$separator を利用して文字列をトークン化します。この際に $leftBound と $rightBound の間に現れる $separator は無視します
このメソッドは、タグリストのような定形フォーマットのデータを分割するのに便利です。
$data = "cakephp 'great framework' php";
$result = String::tokenize($data, ' ', "'", "'");
// 結果
array('cakephp', "'great framework'", 'php');
insert メソッドは、テンプレートとキー・バリューの組み合わせから文字列を作成できます。
String::insert('My name is :name and I am :age years old.', array('name' => 'Bob', 'age' => '65'));
// 生成される文字列: "My name is Bob and I am 65 years old."
与えられた $options に ‘clean’ キーが存在した場合、その指定に従って String::insert をクリーンアップします。 デフォルトでは text を利用しますが、html も用意されています。 この機能の目的は、 Set::insert では取り除けなかったホワイトスペース、 および、プレースホルダー周辺で必要がないマークアップを取り除くことにあります。
オプションは次のように指定します。
$options = array(
'clean' => array(
'method' => 'text', // or html
),
'before' => '',
'after' => ''
);
テキストのブロックを決められた幅や折り返し、インデントにも対応します。 単語の途中で改行されたりしないように、賢く折り返しの処理を行います。
$text = 'This is the song that never ends.';
$result = String::wrap($text, 22);
// 出力
This is the song
that never ends.
どのように折り返し処理を行うか、オプションの配列で指定することができます。 サポートされているオプションは次のとおりです。
パラメタ: |
|
---|
$haystack 中の $needle を $options['format'] で指定された文字列か、デフォルトの文字列でハイライト表示します。
オプション:
例:
// TextHelper として呼び出し
echo $this->Text->highlight($lastSentence, 'using', array('format' => '<span class="highlight">\1</span>'));
// String クラスとして呼び出し
App::uses('String', 'Utility');
echo String::highlight($lastSentence, 'using', array('format' => '<span class="highlight">\1</span>'));
出力:
Highlights $needle in $haystack <span class="highlight">using</span>
the $options['format'] string specified or a default string.
$text の中の HTML リンクを取り除きます。
パラメタ: |
|
---|
文字列を $length の長さでカットします。テキストの長さが $length よりも長かった場合は、 'ending' で指定されたサフィックスを追加します。 もし 'exact' が false の場合、次の単語の最後まで含めて切り取ります。 もし、 'html' が true の場合は HTML タグはカット対象になりません。
$options は、どんな拡張パラメータでも利用できるように使われますが、デフォルトでは次のオプションのみが利用できます。
array(
'ending' => '...',
'exact' => true,
'html' => false
)
例:
// TextHelper として利用
echo $this->Text->truncate(
'The killer crept forward and tripped on the rug.',
22,
array(
'ending' => '...',
'exact' => false
)
);
// String クラスとして利用
App::uses('String', 'Utility');
echo String::truncate(
'The killer crept forward and tripped on the rug.',
22,
array(
'ending' => '...',
'exact' => false
)
);
出力:
The killer crept...
パラメタ: |
|
---|
$haystack から $needle の前後 $radius の数の文字列を抜き出します。 抜き出した文字列に $ending の文字列を前後に付けて返します。 このメソッドは検索結果の表示に特に役立ちます。 検索結果のドキュメント内で、検索文字列やキーワードを示すことができます。
// TextHelper として利用
echo $this->Text->excerpt($lastParagraph, 'method', 50, '...');
// String クラスとして利用
App::uses('String', 'Utility');
echo String::excerpt($lastParagraph, 'method', 50, '...');
出力:
... by $radius, and prefix/suffix with $ending. This method is
especially handy for search results. The query...
パラメタ: |
|
---|
最後の2つの要素をを「and」で結合したカンマ区切りのリストを作成します。
// TextHelper として利用
echo $this->Text->toList($colors);
// String として利用
App::uses('String', 'Utility');
echo String::toList($colors);
出力:
red, orange, yellow, green, blue, indigo and violet