Skip to content

Instantly share code, notes, and snippets.

@fly2xiang
Created November 10, 2016 07:08
Show Gist options
  • Save fly2xiang/fa46cbf302ec40472cbf3a1463a7ac1d to your computer and use it in GitHub Desktop.
Save fly2xiang/fa46cbf302ec40472cbf3a1463a7ac1d to your computer and use it in GitHub Desktop.
将任意内容转换为零宽字符,“隐藏”内容
<?php
/**
* http://ucren.com/blog/archives/549
* 利用零宽字符“隐藏”信息
* 每个字符都有一个唯一的编码,将编码以 2 进制表示得到 01.. 的字串,把 1 替换成 U+200C,把 0 替换成 U+200D 就得到一个全零宽空白的字符串
* 在 unicode 里,至少有 U+200B, U+200C, U+200D 和 U+FEFF 四个零宽字符
*/
function content2width0($content = ''){
$hex = bin2hex($content);
$bin = '';
$hex_len = strlen($hex);
$step = 8;
for($i = 0; $i < $hex_len; $i += $step){
$substr = substr($hex, $i, $step);
$substr_len = strlen($substr);
$padding = min($step, $substr_len) * 4;
$bin .= str_pad(base_convert($substr, 16, 2), $padding, '0', STR_PAD_LEFT);
}
unset($i, $hex_len, $step, $padding, $substr, $substr_len);
$width0 = str_replace(array('0', '1'), array(hex2bin('e2808c'), hex2bin('e2808d')), $bin);
return $width0;
}
function width02content($width0 = ''){
$bin = str_replace(array(hex2bin('e2808c'), hex2bin('e2808d')), array('0', '1'), $width0);
$hex = '';
$bin_len = strlen($bin);
$step = 32;
for($i = 0; $i < $bin_len; $i += $step){
$substr = substr($bin, $i, $step);
$substr_len = strlen($substr);
$padding = floor(min($step, $substr_len) / 4);
$hex .= str_pad(base_convert($substr, 2, 16), $padding, '0', STR_PAD_LEFT);
}
unset($i, $bin_len, $step, $padding, $substr, $substr_len);
$content = hex2bin($hex);
return $content;
}
$content = '字符:§№☆★○●◎◇◆□■△▲※→←↑↓〓#&@\︿_ ̄'.PHP_EOL;
$content .= '!@#$%^&*()'.PHP_EOL;
$content .= '1234567890'.PHP_EOL;
$content .= 'qwertyuiopasdfghjklzxcvbnm'.PHP_EOL;
$content .= '中abc英!@#字☆★○●◎符数123字混排。。”“'.PHP_EOL;
$content .= '!@#$%^&*()_+-=~`\/.,';][{}|、'.PHP_EOL;
$content .= '韩语:컴퓨터';
$content .= '蒙古语:🤣 🤠 🤡 🤥 🤤 🤢 🤧 🤴 🤶 🤵 🤷 🤦 🤰 🕺 🤳 🤞 🤙 🤛 🤜 🤚 🤝 🖤 🦍 ';
$content .= '日语:コンピュータ技術の魔法';
$content .= '✰';
$content .= 'emoji:😃😂😊😉😁😄'; //emoji
$content .= 'emoji2:🤣 🤠 🤡 🤥 🤤 🤢 🤧 🤴 🤶 🤵 🤷 🤦 🤰 🕺 🤳 🤞 🤙 🤛 🤜 🤚 🤝 🖤 🦍 ';
$content .= '===============';
file_put_contents('width0.txt', content2width0($content));
var_dump(width02content(file_get_contents('width0.txt')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment