Source for file typography.php
Documentation is available at typography.php
* PHP Version 5.3.1とCakePHP 1.3.11で動作を確認。
* @author http://purl.org/meta/me/
* @version Typography Helper 1.0.1 2011-09-17
* @license Creative Commons License CC BY (http://creativecommons.org/licenses/by/3.0/)
App::import('Helper', 'Text');
App::import('Core', 'String');
* TypographyHelperクラスはタイポグラフィを手助けするヘルパーです。
* class ExamplesController extends AppController {
* var $helpers = array('Typography');
* List of helpers used by this helper
'/([0-9.]+) ?x ?([0-9.]+)/' =>
'$1×$2',
'/([0-9.]+) ?X ?([0-9.]+)/' =>
'$1×$2',
'/([0-9.]+) ?\* ?([0-9.]+)/' =>
'$1×$2',
'/([0-9]+)\/([0-9]+)/' =>
'<sup>$1</sup>⁄<sub>$2</sub>',
'/([0-9.]+)\^([0-9]+)/' =>
'$1<sup>$2</sup>'
* Default map of accented and special characters to ASCII characters
* @link http://api.cakephp.org/class/inflector
'/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/' =>
'A',
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' =>
'a',
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' =>
'E',
'/è|é|ê|ë|ē|ĕ|ė|ę|ě/' =>
'e',
'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' =>
'I',
'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' =>
'i',
'/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' =>
'O',
'/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' =>
'o',
'/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' =>
'U',
'/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' =>
'u',
'/([0-9.]+) ?°C/' =>
'$1℃',
'/([0-9.]+) ?°F/' =>
'$1℉',
'/No. ([0-9]+)/' =>
'№ $1',
'/([0-9]+) ?(L|l) /' =>
'$1 ℓ ',
'/([0-9]+) ?(µl|µL) /' =>
'$1 ㎕ ',
'/([0-9]+) ?(ml|mL|ML) /' =>
'$1 ㎖ ',
'/([0-9]+) ?(dl|dL|DL) /' =>
'$1 ㎗ ',
'/([0-9]+) ?(kl|kL|KL) /' =>
'$1 ㎘ ',
* Before render callback. beforeRender is called before the view file is rendered.
* Overridden in subclasses.
$this->_uuid =
String::uuid();
* n回連続したダッシュを長いダッシュに変換します。
* $typography->horizontal('-------');
* @param string $string 処理を行う文字列
* @param integer $int 何回連続したダッシュを変換するのか
* @return string n回連続したダッシュが長いダッシュに変換された文字列
'return strtr($matches[0], array("--" => "—", "-" => "—"));'
* $typography->mathCharacters('Multiplication 100x100 Fraction 1/2');
* # Multiplication 100×100 Fraction <sup>1</sup>⁄<sub>2</sub>
* @param string $string 処理を行う文字列
* @return string 数学記号に変換された文字列
* 基本的にはnl2brと同じ処理をしますが特定の要素内での処理は除くことができます。
* $typography->nl2brExcept("foo isn't\n bar\n\n<pre>\n pre Element\n</pre>");
* @param string $string 入力する文字列
* @param array $except 処理を除く要素の配列
* @param boolean $is_xhtml trueの場合はXHTMLに対応した<br />、falseの場合はHTMLに対応した<br>になります。
* @return string 改行がbr要素に変換された文字列
function nl2brExcept($string, $except =
array('pre'), $is_xhtml =
true) {
$newstr =
nl2br($newstr, $is_xhtml);
$newstr =
nl2br($newstr);
* $typography->nl2carriageReturn("foo\nbar");
* @param string $string 処理を行う文字列
* @param return $return リターン記号
* @return string 変換された文字列
return strtr($string, array("\r\n" =>
$return .
"\r\n", "\r" =>
$return .
"\r", "\n" =>
$return .
"\n"));
* $typography->normalizeUrl('hxxp URL ttp://example.com/ or hxxp://example.com/example/');
* # hxxp URL http://example.com/ or http://example.com/example/
* @param string $string 処理を行う文字列
* @return string URLを正規化した文字列
$noPchar =
'[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+';
return preg_replace('/(htp|ttp|tp|hxxp|h\+\+p|h\*\*p||ht_tp|htt|_ttp)(s?:\/\/' .
$noPchar .
')/', 'http$2', $string);
* $typography->normalizeUrlLink('hxxp URL ttp://example.com/ or hxxp://example.com/example/', array('rel' => 'nofollow'));
* # hxxp URL <a href="http://example.com/" rel="nofollow">http://example.com/</a> or <a href="http://example.com/example/" rel="nofollow">http://example.com/example/</a>
* @param string $string 処理を行う文字列
* @param array $options Array of HTML options.
* @return string URLを正規化してリンクに変換された文字列
return $this->autoLink($this->normalizeUrl($string), $options);
* echo $typography->shortenUrl('The Aristotle <a href="http://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%AA%E3%82%B9%E3%83%88%E3%83%86%E3%83%AC%E3%82%B9">http://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%AA%E3%82%B9%E3%83%88%E3%83%86%E3%83%AC%E3%82%B9</a>', 30, '…');
* # The Aristotle <a href="http://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%AA%E3%82%B9%E3%83%88%E3%83%86%E3%83%AC%E3%82%B9">http://ja.wikipedia.org/wiki/%…</a>
* @param string $string 入力する文字列
* @param integer $length 何文字まで切り詰めるか
* @param string $ending 切り詰めた後の文字列
* @return string a要素内のURLを短くされた文字列
function shortenUrl($string, $length =
50, $ending =
null) {
'/(<a.*?>)((https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+?))<\/a>/',
'return $matches[1] . substr($matches[2], 0, ' .
$length .
') . ' .
$ending .
' . "</a>";'
* $typography->specialCharacters('Copyright (C) 2011 example.com All Rights Reserved.');
* # Copyright © 2011 example.com All Rights Reserved.
* @param string $string 処理を行う文字列
* @return string 変換された文字列
* $typography->trim("<p>\r\nWelcome This is my HTML document\r\n</p>");
* # <p>Welcome This is my HTML document</p>
* @param string $string 入力する文字列
* @return string 改行やタブ文字などを消去された文字列
return str_replace(array("\r", "\n", "\t", "\0", "\x0B"), '', $string);
* 基本的にはtrimと同じ処理をしますが特定の要素内での処理は除くことができます。
* $typography->trimExcept("<p>\r\nWelcome This is my HTML document\r\n</p>\r\n<pre>\r\n pre Element\r\n</pre>");
* # <p>Welcome This is my HTML document</p><pre>
* @param string $string 入力する文字列
* @param array $except 処理を除く要素の配列
* @return string 改行やタブ文字などを消去された文字列
function trimExcept($string, $except =
array('pre', 'script')) {
$newstr =
$this->trim($newstr);
* 2回以上連続するスペースとタブ文字を1つのスペースに変換します。
* $typography->trimScrapes("Double Scrape and\tTab");
* # Double Scrape and Tab
* @param string $string 処理を行う文字列
* @return string 変換された文字列
return preg_replace(array('/ {2,}/', '/\\t{1,}/'), ' ', $string);
* 文字列を一行にし、2回以上連続するスペースを1つにします。
* $typography->oneLine("oneLine Test.\r\nDouble Scrape and\tTab");
* # oneLine Test. Double Scrape and Tab
* @param string $string 処理を行う文字列
* @return string 一行に変換された文字列
$string =
str_replace(array("\r", "\n", "\0", "\x0B"), ' ', $string);
* 2回以上連続する改行をp要素に変換します。単一の改行はbr要素に変換します。
* $typography->paragraph("paragraph method Test\r\nbr Elements\r\n\r\np Elements\r\n\r\n\r\nend.");
* # <p>paragraph method Test<br />
* # br Elements</p><p>p Elements</p><p>end.</p>
* @param string $string 処理を行う文字列
* @param array $options p要素のオプション
* @return string 改行がp要素またはbr要素に変換された文字列
function paragraph($string, $options =
array()) {
if(isset
($options['is_xhtml'])) {
$is_xhtml =
$options['is_xhtml'];
unset
($options['is_xhtml']);
foreach($array as $key =>
$value) {
$pElement =
$this->Html->para(null, trim($value), $options);
$out .=
nl2br($pElement, $is_xhtml);
$out .=
nl2br($pElement);
* echo $typography->tabToScrape("tabToScrape\tTest");
* @param string $string 処理を行う文字列
* @param integer $multiplier タブ文字をスペース何個分に変換するか
* @return string タブ文字がスペースに変換された文字列
* @param string $string 処理を行う文字列
* @return string ASCI文字に変換された文字列
* $typography->unitCharacters('100 L 100 °C 100 °C No. 100');
* # 100 ℓ 100℃ 100℃ № 100
* @param string $string 処理を行う文字列
* @return string 単位記号に変換された文字列
$except =
array($except);
if(is_array($value) && isset($value["start"]) && isset($value["end"])) {
return preg_quote($value["start"], "/") . ".*?" . preg_quote($value["end"], "/");
return "<" . $value . ".*?>.*?<\\/" . $value . ">";
return "<" . base64_encode($matches[0]) . "<' .
$this->_uuid .
'>>";
return base64_decode($matches[1]);
Documentation generated on Sat, 17 Sep 2011 04:30:04 +0000 by phpDocumentor 1.4.3