PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/patchwork/utf8/README.md

https://gitlab.com/thugside/clicktocall-laravel
Markdown | 145 lines | 113 code | 32 blank | 0 comment | 0 complexity | b8b4588cf121daafd894b98966722714 MD5 | raw file
  1. Patchwork UTF-8 for PHP
  2. =======================
  3. [![Latest Stable Version](https://poser.pugx.org/patchwork/utf8/v/stable.png)](https://packagist.org/packages/patchwork/utf8)
  4. [![Total Downloads](https://poser.pugx.org/patchwork/utf8/downloads.png)](https://packagist.org/packages/patchwork/utf8)
  5. [![Build Status](https://secure.travis-ci.org/nicolas-grekas/Patchwork-UTF8.png?branch=master)](http://travis-ci.org/nicolas-grekas/Patchwork-UTF8)
  6. [![SensioLabsInsight](https://insight.sensiolabs.com/projects/666c8ae7-0997-4d27-883a-6089ce3cc76b/mini.png)](https://insight.sensiolabs.com/projects/666c8ae7-0997-4d27-883a-6089ce3cc76b)
  7. Patchwork UTF-8 gives PHP developpers extensive, portable and performant
  8. handling of UTF-8 and [grapheme clusters](http://unicode.org/reports/tr29/).
  9. It provides both :
  10. - a portability layer for `mbstring`, `iconv`, and intl `Normalizer` and
  11. `grapheme_*` functions,
  12. - an UTF-8 grapheme clusters aware replica of native string functions.
  13. It can also serve as a documentation source referencing the practical problems
  14. that arise when handling UTF-8 in PHP: Unicode concepts, related algorithms,
  15. bugs in PHP core, workarounds, etc.
  16. Portability
  17. -----------
  18. Unicode handling in PHP is best performed using a combo of `mbstring`, `iconv`,
  19. `intl` and `pcre` with the `u` flag enabled. But when an application is expected
  20. to run on many servers, you should be aware that these 4 extensions are not
  21. always enabled.
  22. Patchwork UTF-8 provides pure PHP implementations for 3 of those 4 extensions.
  23. `pcre` compiled with unicode support is required but is widely available.
  24. The following set of portability-fallbacks allows an application to run on a
  25. server even if one or more of those extensions are not enabled:
  26. - *utf8_encode, utf8_decode*,
  27. - `mbstring`: *mb_check_encoding, mb_convert_case, mb_convert_encoding,
  28. mb_decode_mimeheader, mb_detect_encoding, mb_detect_order,
  29. mb_encode_mimeheader, mb_encoding_aliases, mb_internal_encoding, mb_language,
  30. mb_list_encodings, mb_strlen, mb_strpos, mb_strrpos, mb_strtolower,
  31. mb_strtoupper, mb_stripos, mb_stristr, mb_strrchr, mb_strrichr, mb_strripos,
  32. mb_strstr, mb_substitute_character, mb_substr*,
  33. - `iconv`: *iconv, iconv_mime_decode, iconv_mime_decode_headers,
  34. iconv_get_encoding, iconv_set_encoding, iconv_mime_encode, ob_iconv_handler,
  35. iconv_strlen, iconv_strpos, iconv_strrpos, iconv_substr*,
  36. - `intl`: *Normalizer, grapheme_extract, grapheme_stripos, grapheme_stristr,
  37. grapheme_strlen, grapheme_strpos, grapheme_strripos, grapheme_strrpos,
  38. grapheme_strstr, grapheme_substr, normalizer_is_normalized,
  39. normalizer_normalize*.
  40. Patchwork\Utf8
  41. --------------
  42. [Grapheme clusters](http://unicode.org/reports/tr29/) should always be
  43. considered when working with generic Unicode strings. The `Patchwork\Utf8`
  44. class implements the quasi-complete set of native string functions that need
  45. UTF-8 grapheme clusters awareness. Function names, arguments and behavior
  46. carefully replicates native PHP string functions.
  47. Some more functions are also provided to help handling UTF-8 strings:
  48. - *filter()*: normalizes to UTF-8 NFC, converting from [CP-1252](http://wikipedia.org/wiki/CP-1252) when needed,
  49. - *isUtf8()*: checks if a string contains well formed UTF-8 data,
  50. - *toAscii()*: generic UTF-8 to ASCII transliteration,
  51. - *strtocasefold()*: unicode transformation for caseless matching,
  52. - *strtonatfold()*: generic case sensitive transformation for collation matching
  53. Mirrored string functions are:
  54. *strlen, substr, strpos, stripos, strrpos, strripos, strstr, stristr, strrchr,
  55. strrichr, strtolower, strtoupper, wordwrap, chr, count_chars, ltrim, ord, rtrim,
  56. trim, str_ireplace, str_pad, str_shuffle, str_split, str_word_count, strcmp,
  57. strnatcmp, strcasecmp, strnatcasecmp, strncasecmp, strncmp, strcspn, strpbrk,
  58. strrev, strspn, strtr, substr_compare, substr_count, substr_replace, ucfirst,
  59. lcfirst, ucwords, number_format, utf8_encode, utf8_decode, json_decode,
  60. filter_input, filter_input_array*.
  61. Notably missing (but hard to replicate) are *printf*-family functions.
  62. The implementation favors performance over full edge cases handling.
  63. It generally works on UTF-8 normalized strings and provides filters to get them.
  64. As the turkish locale requires special cares, a `Patchwork\TurkishUtf8` class
  65. is provided for working with this locale. It clones all the features of
  66. `Patchwork\Utf8` but knows about the turkish specifics.
  67. Usage
  68. -----
  69. The recommended way to install Patchwork UTF-8 is [through
  70. composer](http://getcomposer.org). Just create a `composer.json` file and run
  71. the `php composer.phar install` command to install it:
  72. {
  73. "require": {
  74. "patchwork/utf8": "~1.1"
  75. }
  76. }
  77. Then, early in your bootstrap sequence, you have to configure your environment:
  78. ```php
  79. \Patchwork\Utf8\Bootup::initAll(); // Enables the portablity layer and configures PHP for UTF-8
  80. \Patchwork\Utf8\Bootup::filterRequestUri(); // Redirects to an UTF-8 encoded URL if it's not already the case
  81. \Patchwork\Utf8\Bootup::filterRequestInputs(); // Normalizes HTTP inputs to UTF-8 NFC
  82. ```
  83. Run `phpunit` to see the code in action.
  84. Make sure that you are confident about using UTF-8 by reading
  85. [Character Sets / Character Encoding Issues](http://www.phpwact.org/php/i18n/charsets)
  86. and [Handling UTF-8 with PHP](http://www.phpwact.org/php/i18n/utf-8),
  87. or [PHP et UTF-8](http://julp.lescigales.org/articles/3-php-et-utf-8.html) for french readers.
  88. You should also get familiar with the concept of
  89. [Unicode Normalization](http://en.wikipedia.org/wiki/Unicode_equivalence) and
  90. [Grapheme Clusters](http://unicode.org/reports/tr29/).
  91. Do not blindly replace all use of PHP's string functions. Most of the time you
  92. will not need to, and you will be introducing a significant performance overhead
  93. to your application.
  94. Screen your input on the *outer perimeter* so that only well formed UTF-8 pass
  95. through. When dealing with badly formed UTF-8, you should not try to fix it
  96. (see [Unicode Security Considerations](http://www.unicode.org/reports/tr36/#Deletion_of_Noncharacters)).
  97. Instead, consider it as [CP-1252](http://wikipedia.org/wiki/CP-1252) and use
  98. `Patchwork\Utf8::utf8_encode()` to get an UTF-8 string. Don't forget also to
  99. choose one unicode normalization form and stick to it. NFC is now the defacto
  100. standard. `Patchwork\Utf8::filter()` implements this behavior.
  101. This library is orthogonal to `mbstring.func_overload` and will not work if the
  102. php.ini setting is enabled.
  103. Licensing
  104. ---------
  105. Patchwork\Utf8 is free software; you can redistribute it and/or modify it under
  106. the terms of the (at your option):
  107. - [Apache License v2.0](http://apache.org/licenses/LICENSE-2.0.txt), or
  108. - [GNU General Public License v2.0](http://gnu.org/licenses/gpl-2.0.txt).
  109. Unicode handling requires tedious work to be implemented and maintained on the
  110. long run. As such, contributions such as unit tests, bug reports, comments or
  111. patches licensed under both licenses are really welcomed.
  112. I hope many projects could adopt this code and together help solve the unicode
  113. subject for PHP.