Skip to content

Instantly share code, notes, and snippets.

@Go-Noji
Last active April 9, 2018 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Go-Noji/2b462c2d18dd576fc66915c935bcd38e to your computer and use it in GitHub Desktop.
Save Go-Noji/2b462c2d18dd576fc66915c935bcd38e to your computer and use it in GitHub Desktop.
PHPで汎用的な伏字クラスを作成しました ref: https://qiita.com/Go-Noji/items/1c2a0fa4584558281a86
//クラスを読み込む
include_once 'Unprintable.php';
//伏字を'×'、伏字後の文字数を3文字にする
$unprintable = new Unprintable('×', 3);
//都道府県を伏字
$unprintable->add_sentence(array('北海', '青森', '岩手', '宮城', '秋田', '山形', '福島', '茨城', '栃木', '群馬', '埼玉', '千葉', '東京', '神奈川', '新潟', '富山', '石川', '福井', '山梨', '長野', '岐阜', '静岡', '愛知', '三重', '滋賀', '京都', '大阪', '兵庫', '奈良', '和歌山', '鳥取', '島根', '岡山', '広島', '山口', '徳島', '香川', '愛媛', '高知', '福岡', '佐賀', '長崎', '熊本', '大分', '宮崎', '鹿児島', '沖縄'));
//メールアドレスを伏字
$unprintable->add_sentence('([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+', TRUE);
//電話番号(-有り)を伏字
$unprintable->add_sentence('[0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}', TRUE);
//http or https始まりのURLを伏字
$unprintable->add_sentence('(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)', TRUE);
//原文
$txt = 'こんにちわ! 山田 太郎です! 僕の連絡先はhoge@example.comだよ! 電話番号なら090-0000-0000にかけてきてね! 09001234567でも大丈夫だよ! もしよかったら東京に遊びにきなよ^^ 暇だったらここじゃなくてhttps://qiita.com/Go-Nojiでもっとお話ししようよ!';
//置換して出力
echo $unprintable->convert_string($txt);
$last_name = '山田';
$first_name = '太郎';
echo $unprintable->convert_string($txt, array(
array('sentence' => $last_name, 'regex' => FALSE),
array('sentence' => $first_name , 'regex' => FALSE)
));
echo $unprintable->convert_string($txt, array(
array('sentence' => $last_name, 'regex' => FALSE)
));
echo $unprintable->convert_string($txt, array(
array('sentence' => $first_name , 'regex' => FALSE)
));
echo $unprintable->convert_string($txt, array(
array('sentence' => '[0-9]{10,11}', 'regex' => TRUE)
));
<?php
class Unprintable
{
/**
* 伏字にした後の置換文字列
* 置換後はこの文字列を一文字と数え代替される
* @var string
*/
private $substitution;
/**
* 伏字置換時の文字数
* 0にしておくと置換前と同じ文字数になる
* @var int
*/
private $length;
/**
* 伏字にしたい文字列の配列
* 'sentence'が伏字文字列, 'regex'が正規表現かどうかの真偽値
* @var array
*/
private $sentences;
/**
* コンストラクタ
* 伏字置換文字列・文字数の設定
* @param string $substitution
* @param int $length
*/
public function __construct($substitution = '*', $length = 0)
{
$this->substitution = is_string($substitution) && $substitution !== '' ? (string)$substitution : '*';
$this->length = is_int($length) && $length >= 0 ? $length : 0;
}
/**
* $string内に存在する$matchを全て置換して返す
* @param string $match
* @param string $string
* @return string
*/
private function _replace($match, $string)
{
//文字数を計って代替文字を作成し全て置換して返す
return str_replace($match, str_repeat($this->substitution, $this->length ? $this->length : mb_strlen($match)), $string);
}
/**
* 伏字にする文字を追加する
* $sentencesは配列(一次元)か文字列を受け付ける
* $regexをTUREに設定すると正規表現として処理する
* @param array|string $sentences
* @param bool $regex
* @return void
*/
public function add_sentence($sentences, $regex = FALSE)
{
//文字列で来ていたら配列化
if ( ! is_array($sentences))
{
$sentences = array($sentences);
}
//追加
foreach ($sentences as $sentence)
{
$this->sentences[] = array(
'sentence' => (string)$sentence,
'regex' => (bool)$regex
);
}
}
/**
* $stringで渡した文字列を現在の伏字設定を基に加工して返す
* $flashConfigはその場限りの追加設定で、配列の中に'sentence'と'regex'を持った
* 配列を並べた値を指定する
* @param string $string
* @param array $flash_config
* @return string
*/
public function convert_string($string, $flash_config = array())
{
//返す文字列の初期設定
$result = (string)$string;
//設定済伏字設定と一時伏字設定を合体してループ処理
$sentences = array_merge($this->sentences, $flash_config);
foreach ($sentences as $sentence)
{
//regexとsentenceがあるか検査
if ( ! isset($sentence['regex']) || ! isset($sentence['sentence']))
{
die('伏字設定が正しくありません');
}
//正規表現で処理するかどうか
if ($sentence['regex'])
{
//マッチする文字列の取得兼ループ
$limit = 100;
while (preg_match('{'.$sentence['sentence'].'}', $result, $match) && $limit > 0)
{
//正規表現置換
$result = $this->_replace($match[0], $result);
--$limit;
}
continue;
}
//通常置換
$result = $this->_replace($sentence['sentence'], $result);
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment