Created
November 5, 2012 04:43
-
-
Save s-hiroshi/4015382 to your computer and use it in GitHub Desktop.
PHP > エスケープ処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HTMLエンティティへエンコード(エスケープ)及びデコード | |
*/ | |
class EscapeUtility { | |
/** | |
* 文字列をHTMLエスケープする | |
* | |
* @param string $string エスケープする文字列 | |
* @return string エスケープされた文字列 | |
*/ | |
public static function escape($string) | |
{ | |
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); | |
} | |
/** | |
* aタグとemタグを除いてエスケープする。 | |
*/ | |
public static function escapeTextarea($text) { | |
// aタグとemタグだけ許可する | |
$result = htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); | |
$result = self::decodeA($result); | |
$result = self::decodeEm($result); | |
$result = self::decodeQuoteA($result); | |
return $result; | |
} | |
/** | |
* aタグのHTMLエンティティ <, >を<,>へデコード | |
* | |
* @param string $text デコードする文字列 | |
* @return string デコードした文字列 | |
*/ | |
private static function decodeA($text) { | |
// 正規表現は最小マッチ | |
// >a...<, >/a< は<a...>, </a>へ置換 | |
return preg_replace('/<(\/?a.*?)>/i', '<$1>', $text); | |
} | |
/** | |
* emタグのHTMLエンティティ <, >を<,>へデコード | |
* | |
* @param string $text デコードする文字列 | |
* @return string デコードした文字列 | |
*/ | |
private static function decodeEm($text) { | |
// 正規表現は最小マッチ | |
// >em<, >/em<は<em>, </em>へ置換 | |
return preg_replace('/<(\/?em*?)>/i', '<$1>', $text); | |
} | |
/** | |
* aタグのhref属性値の引用符"を"デコード | |
* | |
* @param $text デコードする文字列 | |
* @return string デコードした文字列 | |
*/ | |
private static function decodeQuoteA($text) { | |
// a href="ehttp://..."eをデコード | |
// URL構成文字 [^;/?:@&=+\$,A-Za-z0-9\-_.!~*'()%] | |
// パターンにiオプションを付けているので[^;/?:@&=+\$,a-z0-9\-_.!~*'()%]でよい。 | |
$pattern = "/(a href=)"(https?:\/\/[a-z0-9_,.:;&=+*%$#!?@()~\'\/-]+?)"/i"; | |
$replacement = '$1"$2"'; | |
return preg_replace($pattern, $replacement, $text); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// エスケープ処理 | |
require_once('escape.php'); | |
// textareaで使用可能なタグa, em。 | |
$result = EscapeUtility::escapeTextarea($_POST['textarea']); | |
?> | |
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>EscapeUtilityの確認</title> | |
<meta name="description" content=""> | |
<meta name="keywords" content=""> | |
<style> | |
form { | |
margin-bottom: 2em; | |
} | |
textarea { | |
width: 800px; | |
} | |
#output { | |
width: 760px; | |
padding: 20px; | |
margin-bottom: 2em; | |
background: #E4E4E4; | |
} | |
</style> | |
</head> | |
<body> | |
<form action="index.php" method="POST"> | |
<textarea id="input" name="textarea"></textarea><br> | |
<input type="submit" value="送信"> | |
</form> | |
<div id="output"><?php echo $result; ?></div> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment