Skip to content

Commit

Permalink
add pinyin & chaos doc
Browse files Browse the repository at this point in the history
  • Loading branch information
rozbo committed Mar 17, 2017
1 parent fc3bc19 commit 47a19f5
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 1 deletion.
180 changes: 180 additions & 0 deletions core/helpers/Chaos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
namespace puck\helpers;
class Chaos {
public $GbToBigArray;
public $_pinyin;
public $string;
public $url = '';
public $msg = '';
public $action;
private $prefix;

function __construct(){
$this->_pinyin = new PinYin();
}

/**
* @param array $prefix
*/
public function addPrefix(array $prefix) {
$this->prefix=$prefix[array_rand($prefix)];
}

public function clearPrefix() {
$this->prefix='';
}
public function get($str) {
$this->cutting($str);
$this->convert();
$this->clearPrefix();
return $this->msg;
}

//分离URL
private function cutting($str){

if(preg_match("@(http://[^\\s]+)@i", $str, $result)) {
$this->url = $result[1];
$this->msg = $str = str_replace($this->url, '%%%%', $str);
} else {
$this->msg = $str;
}
}



private function convert( $method = 'msg' ){
if( $method == 'msg' || $method == 'all') {
$this->msg = $this->setPinyin($this->msg);
$this->msg = $this->setRepeat($this->msg);
//$this->msg = $this->GbToBig($this->msg);
$this->msg = $this->setBlankness($this->msg);

}
if( $method == 'url' || $method == 'all') {
$this->url = $this->setChacha($this->url);
}
$this->msg=$this->prefix.str_replace( '%%%%', $this->url, $this->msg);
}

function setChacha($url ){
$url = strtolower($url);
$arr = array(
'a' => array('a','A','','','Α','А','α'),
'b' => array('b','B','','','Β','В','Ь'),
'c' => array('c','C','','','С','с'),
'd' => array('d','D','',''),
'e' => array('e','E','','','Ε','Е','е'),
'f' => array('f','F','',''),
'g' => array('g','G','',''),
'h' => array('h','H','','','Η','Н','н'),
'i' => array('i','I','','','Ι',''),
'j' => array('j','J','',''),
'k' => array('k','K','','','Κ','κ','к','К'),
'l' => array('l','L','','','','','|'),
'm' => array('m','M','','','Μ','М','м'),
'n' => array('n','N','','','Ν',''),
'o' => array('o','O','','','Ο','О'),
'p' => array('p','P','','','Ρ','Р','р'),
'q' => array('q','Q','',''),
'r' => array('r','R','',''),
's' => array('s','S','',''),
't' => array('t','T','','','Τ','Т',''),
'u' => array('u','U','','',''),
'v' => array('v','V','','','','ν'),
'w' => array('w','W','',''),
'x' => array('x','X','','','Χ','χ','Х','х','','×'),
'y' => array('y','Y','','','У'),
'z' => array('z','Z','','','Ζ'),

'1' => array('1',''),
'2' => array('2',''),
'3' => array('3',''),
'4' => array('4',''),
'5' => array('5',''),
'6' => array('6',''),
'7' => array('7',''),
'8' => array('8',''),
'9' => array('9',''),
'0' => array('0',''),

':' => array(':','',''),
'/' => array('/',''),
'.' => array('','·','','','','','')

);
$len = strlen( $url );
$temp = "\n\n";
for( $i=0; $i<$len; $i++){
$t_str = substr( $url, $i, 1 );
$sj = mt_rand( 0, count($arr[$t_str])-1 );
$temp .= $arr[$t_str][$sj];
}
return $temp;
}

//随机把一个字符转为拼音
function setPinyin($str) {
$py = mt_rand(0, iconv_strlen( $str, 'UTF-8' )-1);
$t_str = iconv_substr( $str, $py, 1, 'UTF-8');
if(mt_rand(0,10) > 5) $pinyin = " ";
$pinyin = $this->_pinyin->convert($t_str,PINYIN_UNICODE);
$pinyin=implode(" ",$pinyin);
if(mt_rand(0,10) > 5) $pinyin .= " ";
if($t_str != "%"){
$str = preg_replace("'$t_str'", $pinyin, $str, 1);
}
return $str;
}

//随机重复一个字符
function setRepeat($str) {
$len = iconv_strlen( $str, 'UTF-8' );
$action = 0;
$temp = '';
for( $i=0; $i<$len; $i++ ){
$t_str = iconv_substr( $str, $i, 1 ,'UTF-8');
if( mt_rand( 1, 50 ) > 48 && $action == 0) {
if(!preg_match("@[a-z0-9%\\s]+@i", $t_str)) {
$temp .= $t_str;
$action = 1;
}
}
$temp .= $t_str;
}
return $temp;
}

//随机插入不影响阅读的字符
function setBlankness($str) {
$blankness = array(" ", ' ', '҉','̅̅','̲','̲̲','̅','̲̲̅̅');
$len = iconv_strlen( $str, 'UTF-8' );
$temp = '';
for( $i=0; $i<$len; $i++ ){
$t_str = iconv_substr( $str, $i, 1 ,'UTF-8');
if( mt_rand( 1, 50 ) > 48 ) {
if(!preg_match("@[a-z0-9%\\s]+@i", $t_str)) {
$temp .= $blankness[mt_rand(0, 7)];
}
}
$temp .= $t_str;
}
return $temp;
}

//随机进行繁体中文转换
function GbToBig( $str ){
$len = iconv_strlen( $str, 'UTF-8' );
$temp = '';
for( $i=0; $i<$len; $i++ ){
$t_str = iconv_substr( $str, $i, 1 ,'UTF-8');
if( mt_rand( 1, 50 ) > 48 ) {
$t_str = strtr( $t_str, $this->GbToBigArray );
}
$temp .= $t_str;
}
return $temp;
}
}


3 changes: 2 additions & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
- [视图](/view)
- [异常](/exception)
-  拓展
- 待添加
- [拼音转换](/pinyin)
- [字符打乱](/chaos)
37 changes: 37 additions & 0 deletions docs/chaos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## 字符打乱
`puck`可以支持生成添加干扰符号,字符码等的变形字体,以实现个性的效果。 

## 用法

### 基本用法
```php
use puck\helpers\Chaos;
$s='你好';
$chaos = new Chaos();
$prefix=[
'[微笑]',
'[大笑]',
'[色]',
'[失望]',
'[皱眉]',
'[哭]',
'[惊恐]',
'[亲亲]',
'[大眼]',
'[抓狂]',
'[脸红]',
'[书呆子]',
'[可爱]',
'[吐舌]',
'[眨眼]',
'[好吃]',
'[天使]',
'[得意]',
'[讽刺]',
'[花痴]',
'[惊讶]'
];
$chaos->addPrefix($prefix);
echo $chaos->get($s);
//[惊讶]ni好
```
56 changes: 56 additions & 0 deletions docs/pinyin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## 拼音扩展
`puck`支持中文和拼音间的转换,对地名,人名,句子等均有处理,它甚至能支持音调的携带。
这个扩展库由[https://github.com/overtrue/pinyin](https://github.com/overtrue/pinyin)提供技术支持。

## 用法

### 基本用法
```php
use puck\helpers\Pinyin;
$pinyin = new Pinyin();
$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_UNICODE);
// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]

$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII);
//["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]

```

### 生产用于链接

```php
$pinyin->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lv-xing
$pinyin->permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lv.xing
```

### 获取首字符

```php
$pinyin->abbr('带着希望去旅行'); // dzxwqlx
$pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x
```

### 生成句子

```php
$pinyin->sentence('带着希望去旅行,比到达终点更美好!');
// dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao!

$pinyin->sentence('带着希望去旅行,比到达终点更美好!', true);
// dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
```

### 翻译姓名

```php
$pinyin->name('单某某'); // ['shan', 'mou', 'mou']
$pinyin->name('单某某', PINYIN_UNICODE); // ["shàn","mǒu","mǒu"]
```

### 专用名词

```php
$pinyin->noun('山西'); // ShanXi
```

0 comments on commit 47a19f5

Please sign in to comment.