Advertisement
TachyonVortex

mbstring iconv script

Feb 6th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <?php
  2.  
  3. function getPropertyFromPhpInfo($pattern)
  4. {
  5.     ob_start();
  6.     phpinfo();
  7.     $phpinfo = ob_get_clean();
  8.  
  9.     $result = preg_match($pattern, $phpinfo, $matches);
  10.  
  11.     if ($result !== 1) {
  12.         return null;
  13.     }
  14.  
  15.     return $matches[0];
  16. }
  17.  
  18. function getIconvEncodings()
  19. {
  20.     $encodings = shell_exec('/usr/bin/iconv --list');
  21.     $encodings = strtolower(trim($encodings));
  22.     $encodings = str_replace('//', '', $encodings);
  23.     $encodings = preg_split('/\s+/', $encodings);
  24.  
  25.     return $encodings;
  26. }
  27.  
  28. function getMbstringEncodings()
  29. {
  30.     $encodings = mb_list_encodings();
  31.     $encodings = array_map('strtolower', $encodings);
  32.  
  33.     return $encodings;
  34. }
  35.  
  36. function makeTableRow($encoding, $iconv, $mbstring)
  37. {
  38.     $output  = sprintf('%20s', $encoding) . '    ';
  39.     $output .= sprintf('%-5s', $iconv   ) . '    ';
  40.     $output .= sprintf('%-5s', $mbstring) . PHP_EOL;
  41.  
  42.     return $output;
  43. }
  44.  
  45.  
  46. error_reporting(E_ALL & ~E_NOTICE);
  47. date_default_timezone_set('UTC');
  48.  
  49. echo getPropertyFromPhpInfo('/PHP Version => [0-9.]+/'           ) . PHP_EOL;
  50. echo getPropertyFromPhpInfo('/Multibyte string engine => [a-z]+/') . PHP_EOL;
  51. echo getPropertyFromPhpInfo('/libmbfl version => [0-9.]+/'       ) . PHP_EOL;
  52. echo getPropertyFromPhpInfo('/iconv implementation => [a-z]+/'   ) . PHP_EOL;
  53. echo getPropertyFromPhpInfo('/iconv library version => [0-9.]+/' ) . PHP_EOL . PHP_EOL;
  54.  
  55. $encodings = [
  56.     'iconv'    => getIconvEncodings(),
  57.     'mbstring' => getMbstringEncodings()
  58. ];
  59.  
  60. foreach ($encodings as $lib => $encs) {
  61.     echo $lib . ' encodings:' . PHP_EOL;
  62.     print_r($encs);
  63.     echo PHP_EOL;
  64. }
  65.  
  66. $commonEncodings = array_intersect($encodings['iconv'], $encodings['mbstring']);
  67.  
  68. sort($commonEncodings);
  69.  
  70. define('TOTAL_BYTES', 1000);
  71.  
  72. $data = openssl_random_pseudo_bytes(TOTAL_BYTES);
  73.  
  74. echo 'Common encodings, with calculated lengths for ' . TOTAL_BYTES . ' bytes:' . PHP_EOL;
  75. echo makeTableRow('ENCODING', 'ICONV', 'MBSTRING');
  76.  
  77. foreach ($commonEncodings as $encoding) {
  78.  
  79.     echo makeTableRow(
  80.         $encoding,
  81.         iconv_strlen($data, $encoding . '//IGNORE'),  // From Patchwork\PHP\Shim\Mbstring
  82.         mb_strlen($data, $encoding)
  83.     );
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement