Textヘルパー

Textヘルパーには、テキスト処理に関する便利な機能があります。リンク付与やテキストの抜粋・ハイライトや切り取り処理など。

ソース:/cake/libs/view/helpers/text.php

autoLinkEmails

テキスト内のメールアドレスを抽出して、リンクを付ける
[php]
<?php
$my_text = 'For more information regarding our world-famous pastries and desserts, contact info@example.com';
echo $this->Text->autoLinkEmails($my_text);
?>
// HTML
For more information regarding our world-famous pastries and desserts,
contact <a href="mailto:info@example.com">info@example.com</a>
[/php]

autoLinkEmails(string $text, array $htmlOptions=array())

  • $text:対象のテキスト
  • $htmlOptions($array()):HTMLのオプション

autoLinkUrls

テキスト内のリンクを抽出して、リンクを付ける (https, http, ftp, nntpが対象)

autoLinkUrls(string $text, array $htmlOptions=array())

highlight

特定のテキストをハイライト表示する
[php]
<?php
$last_sentence = 'Highlights $needle in $haystack using the $options['format'] string specified or a default string.';
echo $this->Text->highlight($last_sentence, 'using',array('format'=>'<span class="highlight">\1</span>');
?>
// HTML
Highlights $needle in $haystack <span class="highlight">using</span> the $options['format'] string specified or a default string.
[/php]

highlight(string $haystack, string $needle, array $options = array() )
オプション
'format':(文字列) ハイライトするhtmlコード
'html':(true/false) trueの場合は、HTMLタグは無視される

"look for a needle in a haystack"で「干し草の山の中にある1本の針を探す」という意味になる。

autoLink

テキスト内のリンク(https, http, ftp, nntp)とメールアドレスを抽出して、リンクを付ける。

autoLink(string $text, array $htmlOptions=array())

$htmlOptionsの使い方によっては、使える?

excerpt

テキスト内から抜粋する
[php]
<?php
$last_paragraph =
'Extracts an excerpt from $haystack surrounding the $needle with a number of characters on each side determined by $radius, and suffixed with $ending.
This method is especially handy for search results.
The query string or keywords can be shown within the resulting document.';
echo $this->Text->excerpt(
$last_paragraph,
'method',
50
);
?>
// HTML
mined by $radius, and suffixed with $ending. This method is especially handy for search results. The query...
[/php]

excerpt(string $haystack, string $needle, int $radius=100, string $ending="...")

対象のテキスト$haystackから$needleを中心にして前後文字数($radius)

stripLinks

テキスト内のリンクをはずす。

stripLinks($text)

toList

配列データからカンマ区切りのリストを出力する。最後の2項目は"and"で出力する。
[php]
<?php
echo $this->Text->toList($colors);
?>
// HTML
red, orange, yellow, green, blue, indigo and violet
[/php]

toList(array $list, $and='and', $separator=', ')

truncate

長くなったテキストを指定の長さに切り取ります。
どうやら、省略後の文字数(&length)には'ending'の文字列分も含まれるようです。"..."とすると最大47文字表示になります。
[php]
<?php
$my_text = '';
echo $this->Text->truncate(
$my_text, // 対象のテキスト
50, // トランケートする文字数
array(
'ending' => '...',// (デフォルト:'...') テキストの終わりを表すサイン
'exact' => true // (デフォルト:true) falseの場合、単語の途中で切り取らず単語の終りで区切る
'html' => false // (デフォルト:false) trueの場合、htmlタグを切り取らない
)
);
?>
[/php]

  • このエントリーをはてなブックマークに追加

関連記事

メール送信(qdmail)

CakePHP(+qdmail)でメール送信する処理を実装しました。 準備 2つのライブラリ(.php)をダウンロードし、以下のように配置して下さい。 app/controllers/compo

no image

シンプルに設置できる数字Captcha「MathCaptcha」

スパム防止などで利用されているCaptchaですが、数字版で使えるものがないか探してみました。 この「MathCapthca」は非常にシンプルに設置できるのはいいですが、やっぱりクエスチョンの部分は

ログインに追加の条件を付与する「userScope」

「ユーザ登録」の続きです。 ユーザ登録後、activate(statusを0に設定するを)せずに「仮登録」のままで、正しいusernameとpasswordでログインを試したところ認証に引っかかって

no image

独自のバリデーションルール

CakePHPで組み込みバリデーションをつくる方法は色々あります。 参考:Data Validation — CakePHP Cookbook v1.3 documentation 上記リンク内に

コントローラ内でバリデーション処理を呼び出す

通常、saveメソッドの際にバリデーション処理も自動で行われますが、save処理と切り離してバリデーションを行うこともできます。このときは、save時と若干異なる処理体系になります。 バリデーシ

no image

recursive設定によるfind()性能改善

CakePHPでは、モデルにアソシエーションを設定している場合、recursive(=>joinする階層)はデフォルトで0に設定されています。「recursiveゼロ」の意味するところとは、「1跨ぎま

no image

ログローテーション

CakePHP1.3では標準ではログはタイプごとに出力されるだけで、定期的なローテーションを行ってくれません。放っておくとひたすら1つのファイルにアペンドされていきます。app/tmp/logs以下に

no image

Htmlヘルパー

ソース:\cake\libs\view\helpers\html.php charset / 文書の文字コードを設定する <?php echo $this->Html-

no image

Sessionコンポーネント

ソース: /cake/libs/controller/components/session.php read read($name = null) セッションの情報を読み込みます。 $

no image

ユーザ登録(仮登録・メール・本登録)

ここ最近は、メールアドレスだけでなく、SNSのアカウントと連携してユーザ登録することもできるWebサービスが増えてきましたね。ユーザは割合としてどちらを選んでるのか気になるところですが。私はできるだけ

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

    PAGE TOP ↑