1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Text |
4
|
|
|
* |
5
|
|
|
* テキスト操作のためのファンクション群 |
6
|
|
|
* |
7
|
|
|
* @package risoluto |
8
|
|
|
* @author Risoluto Developers |
9
|
|
|
* @license http://opensource.org/licenses/bsd-license.php new BSD license |
10
|
|
|
* @copyright (C) 2008-2015 Risoluto Developers / All Rights Reserved. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
//------------------------------------------------------// |
14
|
|
|
// 名前空間の定義 |
15
|
|
|
//------------------------------------------------------// |
16
|
|
|
namespace Risoluto; |
17
|
|
|
|
18
|
|
|
class Text |
19
|
|
|
{ |
20
|
|
|
//------------------------------------------------------// |
21
|
|
|
// クラスメソッド定義 |
22
|
|
|
//------------------------------------------------------// |
23
|
|
|
/** |
24
|
|
|
* __construct() |
25
|
|
|
* |
26
|
|
|
* コンストラクタ |
27
|
|
|
*/ |
28
|
|
|
private function __construct() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* autoUrlLink($target, $newWindow = true, $extra = null) |
34
|
|
|
* |
35
|
|
|
* 引数で指定された文字列中の特定部分をリンクに変換する |
36
|
|
|
* |
37
|
|
|
* @access public |
38
|
|
|
* |
39
|
|
|
* @param string $target 対象となる文字列 |
40
|
|
|
* @param boolean $newWindow 新規ウィンドウモード:true(新規ウィンドウでオープン)) |
41
|
|
|
* @param string $extra リンクタグに付与するアトリビュート(デフォルト:null) |
42
|
|
|
* |
43
|
|
|
* @return string 変換後の文字列 |
44
|
|
|
*/ |
45
|
|
|
public static function autoUrlLink( $target, $newWindow = true, $extra = null ) |
46
|
|
|
{ |
47
|
|
|
// 一度、一時変数へ格納する |
48
|
|
|
$tmp_target = $target; |
49
|
|
|
|
50
|
|
|
// リンクタグのベースを組み立てる |
51
|
|
|
$tmp_replace_text = "<a href='$0'" |
52
|
|
|
. ( $newWindow ? " target='_blank'" : "" ) |
53
|
|
|
. ( !empty( $extra ) ? " " . $extra : "" ) |
54
|
|
|
. ">$0</a>"; |
55
|
|
|
|
56
|
|
|
// 文字列中の「http」又は「https」で始まる部分を、<a>タグに変換する |
57
|
|
|
$tmp_target = preg_replace( "/(http|https)\:\/\/[[:alnum:]-_.!~*'();\/?:@&=+$,%#]+/i", $tmp_replace_text, |
58
|
|
|
$tmp_target ); |
59
|
|
|
|
60
|
|
|
// タグの途中で改行が入っている場合、取り除く |
61
|
|
|
$tmp_target = preg_replace( "/(\r|\n|\r\n)'>/i", "'>", $tmp_target ); |
62
|
|
|
$tmp_target = preg_replace( "/(\r|\n|\r\n)<\/a>/i", "</a>", $tmp_target ); |
63
|
|
|
|
64
|
|
|
return $tmp_target; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* checkFalseVal($value, $default = '', $strict = false) |
69
|
|
|
* |
70
|
|
|
* 引数で指定された値がfalse判定されるか否かを判定し、判定される場合は引数で指定されたデフォルト値をそうでない場合は元の値を返す |
71
|
|
|
* |
72
|
|
|
* @access public |
73
|
|
|
* |
74
|
|
|
* @param string $value 検査対象となる値 |
75
|
|
|
* @param string $default デフォルト値 |
76
|
|
|
* @param boolean $strict 厳密な判定を行うか(falseと空文字列等を区別するか) |
77
|
|
|
* |
78
|
|
|
* @return boolean 引数で指定された元の値または指定されたデフォルト値 |
79
|
|
|
*/ |
80
|
|
|
public static function checkFalseVal( $value, $default = '', $strict = false ) |
81
|
|
|
{ |
82
|
|
|
// 厳密判定を実施するかどうかでチェックを変える |
83
|
|
|
if ($strict) { |
84
|
|
|
return ( $value !== false ) ? $value : $default; |
85
|
|
|
} else { |
86
|
|
|
return ( !empty( $value ) ) ? $value : $default; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |