Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
[//]: # (AUTO-GENERATED BY "PHP README Helper": base file -> docs/base.md)
2
[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)
3
 
4
[![Build Status](https://github.com/voku/portable-ascii/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/voku/portable-ascii/actions)
5
[![Build status](https://ci.appveyor.com/api/projects/status/gnejjnk7qplr7f5t/branch/master?svg=true)](https://ci.appveyor.com/project/voku/portable-ascii/branch/master)
6
[![codecov.io](https://codecov.io/github/voku/portable-ascii/coverage.svg?branch=master)](https://codecov.io/github/voku/portable-ascii?branch=master)
7
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/997c9bb10d1c4791967bdf2e42013e8e)](https://www.codacy.com/app/voku/portable-ascii)
8
[![Latest Stable Version](https://poser.pugx.org/voku/portable-ascii/v/stable)](https://packagist.org/packages/voku/portable-ascii)
9
[![Total Downloads](https://poser.pugx.org/voku/portable-ascii/downloads)](https://packagist.org/packages/voku/portable-ascii)
10
[![License](https://poser.pugx.org/voku/portable-ascii/license)](https://packagist.org/packages/voku/portable-ascii)
11
[![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/moelleken)
12
[![Donate to this project using Patreon](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/voku)
13
 
14
# 🔡 Portable ASCII
15
 
16
## Description
17
 
18
It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your server.
19
 
20
The benefit of Portable ASCII is that it is easy to use, easy to bundle.
21
 
22
The project based on ...
23
+ Sean M. Burke's work (https://metacpan.org/pod/Text::Unidecode)
24
+ Tomaz Solc's work (https://pypi.org/project/Unidecode/)
25
+ Portable UTF-8 work (https://github.com/voku/portable-utf8)
26
+ Daniel St. Jules's work (https://github.com/danielstjules/Stringy)
27
+ Johnny Broadway's work (https://github.com/jbroadway/urlify)
28
+ and many cherry-picks from "github"-gists and "Stack Overflow"-snippets ...
29
 
30
## Index
31
 
32
* [Alternative](#alternative)
33
* [Install](#install-portable-ascii-via-composer-require)
34
* [Why Portable ASCII?](#why-portable-ascii)
35
* [Requirements and Recommendations](#requirements-and-recommendations)
36
* [Usage](#usage)
37
* [Class methods](#class-methods)
38
* [Unit Test](#unit-test)
39
* [License and Copyright](#license-and-copyright)
40
 
41
## Alternative
42
 
43
If you like a more Object Oriented Way to edit strings, then you can take a look at [voku/Stringy](https://github.com/voku/Stringy), it's a fork of "danielstjules/Stringy" but it used the "Portable ASCII"-Class and some extra methods.
44
 
45
```php
46
// Portable ASCII
47
use voku\helper\ASCII;
48
ASCII::to_transliterate('déjà σσς iıii'); // 'deja sss iiii'
49
 
50
// voku/Stringy
51
use Stringy\Stringy as S;
52
$stringy = S::create('déjà σσς iıii');
53
$stringy->toTransliterate();              // 'deja sss iiii'
54
```
55
 
56
## Install "Portable ASCII" via "composer require"
57
```shell
58
composer require voku/portable-ascii
59
```
60
 
61
##  Why Portable ASCII?[]()
62
I need ASCII char handling in different classes and before I added this functions into "Portable UTF-8",
63
but this repo is more modular and portable, because it has no dependencies.
64
 
65
## Requirements and Recommendations
66
 
67
*   No extensions are required to run this library. Portable ASCII only needs PCRE library that is available by default since PHP 4.2.0 and cannot be disabled since PHP 5.3.0. "\u" modifier support in PCRE for ASCII handling is not a must.
68
*   PHP 7.0 is the minimum requirement
69
*   PHP 8.0 is also supported
70
 
71
## Usage
72
 
73
Example: ASCII::to_ascii()
74
```php
75
  echo ASCII::to_ascii('�Düsseldorf�', 'de');
76
 
77
  // will output
78
  // Duesseldorf
79
 
80
  echo ASCII::to_ascii('�Düsseldorf�', 'en');
81
 
82
  // will output
83
  // Dusseldorf
84
```
85
 
86
# Portable ASCII | API
87
 
88
The API from the "ASCII"-Class is written as small static methods.
89
 
90
 
91
## Class methods
92
 
93
<p id="voku-php-readme-class-methods"></p><table><tr><td><a href="#charsarraybool-replace_extra_symbols-array">charsArray</a>
94
</td><td><a href="#charsarraywithmultilanguagevaluesbool-replace_extra_symbols-array">charsArrayWithMultiLanguageValues</a>
95
</td><td><a href="#charsarraywithonelanguagestring-language-bool-replace_extra_symbols-bool-asorigreplacearray-array">charsArrayWithOneLanguage</a>
96
</td><td><a href="#charsarraywithsinglelanguagevaluesbool-replace_extra_symbols-bool-asorigreplacearray-array">charsArrayWithSingleLanguageValues</a>
97
</td></tr><tr><td><a href="#cleanstring-str-bool-normalize_whitespace-bool-keep_non_breaking_space-bool-normalize_msword-bool-remove_invisible_characters-string">clean</a>
98
</td><td><a href="#getalllanguages-string">getAllLanguages</a>
99
</td><td><a href="#is_asciistring-str-bool">is_ascii</a>
100
</td><td><a href="#normalize_mswordstring-str-string">normalize_msword</a>
101
</td></tr><tr><td><a href="#normalize_whitespacestring-str-bool-keepnonbreakingspace-bool-keepbidiunicodecontrols-bool-normalize_control_characters-string">normalize_whitespace</a>
102
</td><td><a href="#remove_invisible_charactersstring-str-bool-url_encoded-string-replacement-bool-keep_basic_control_characters-string">remove_invisible_characters</a>
103
</td><td><a href="#to_asciistring-str-string-language-bool-remove_unsupported_chars-bool-replace_extra_symbols-bool-use_transliterate-boolnull-replace_single_chars_only-string">to_ascii</a>
104
</td><td><a href="#to_ascii_remapstring-str1-string-str2-string">to_ascii_remap</a>
105
</td></tr><tr><td><a href="#to_filenamestring-str-bool-use_transliterate-string-fallback_char-string">to_filename</a>
106
</td><td><a href="#to_slugifystring-str-string-separator-string-language-string-replacements-bool-replace_extra_symbols-bool-use_str_to_lower-bool-use_transliterate-string">to_slugify</a>
107
</td><td><a href="#to_transliteratestring-str-stringnull-unknown-bool-strict-string">to_transliterate</a>
108
</td></tr></table>
109
 
110
#### charsArray(bool $replace_extra_symbols): array
111
<a href="#voku-php-readme-class-methods">↑</a>
112
Returns an replacement array for ASCII methods.
113
 
114
EXAMPLE: <code>
115
$array = ASCII::charsArray();
116
var_dump($array['ru']['б']); // 'b'
117
</code>
118
 
119
**Parameters:**
120
- `bool $replace_extra_symbols [optional] <p>Add some more replacements e.g. "£" with " pound ".</p>`
121
 
122
**Return:**
123
- `array`
124
 
125
--------
126
 
127
#### charsArrayWithMultiLanguageValues(bool $replace_extra_symbols): array
128
<a href="#voku-php-readme-class-methods">↑</a>
129
Returns an replacement array for ASCII methods with a mix of multiple languages.
130
 
131
EXAMPLE: <code>
132
$array = ASCII::charsArrayWithMultiLanguageValues();
133
var_dump($array['b']); // ['β', 'б', 'ဗ', 'ბ', 'ب']
134
</code>
135
 
136
**Parameters:**
137
- `bool $replace_extra_symbols [optional] <p>Add some more replacements e.g. "£" with " pound ".</p>`
138
 
139
**Return:**
140
- `array <p>An array of replacements.</p>`
141
 
142
--------
143
 
144
#### charsArrayWithOneLanguage(string $language, bool $replace_extra_symbols, bool $asOrigReplaceArray): array
145
<a href="#voku-php-readme-class-methods">↑</a>
146
Returns an replacement array for ASCII methods with one language.
147
 
148
For example, German will map 'ä' to 'ae', while other languages
149
will simply return e.g. 'a'.
150
 
151
EXAMPLE: <code>
152
$array = ASCII::charsArrayWithOneLanguage('ru');
153
$tmpKey = \array_search('yo', $array['replace']);
154
echo $array['orig'][$tmpKey]; // 'ё'
155
</code>
156
 
157
**Parameters:**
158
- `ASCII::* $language [optional] <p>Language of the source string e.g.: en, de_at, or de-ch.
159
(default is 'en') | ASCII::*_LANGUAGE_CODE</p>`
160
- `bool $replace_extra_symbols [optional] <p>Add some more replacements e.g. "£" with " pound ".</p>`
161
- `bool $asOrigReplaceArray [optional] <p>TRUE === return {orig: string[], replace: string[]}
162
array</p>`
163
 
164
**Return:**
165
- `array <p>An array of replacements.</p>`
166
 
167
--------
168
 
169
#### charsArrayWithSingleLanguageValues(bool $replace_extra_symbols, bool $asOrigReplaceArray): array
170
<a href="#voku-php-readme-class-methods">↑</a>
171
Returns an replacement array for ASCII methods with multiple languages.
172
 
173
EXAMPLE: <code>
174
$array = ASCII::charsArrayWithSingleLanguageValues();
175
$tmpKey = \array_search('hnaik', $array['replace']);
176
echo $array['orig'][$tmpKey]; // '၌'
177
</code>
178
 
179
**Parameters:**
180
- `bool $replace_extra_symbols [optional] <p>Add some more replacements e.g. "£" with " pound ".</p>`
181
- `bool $asOrigReplaceArray [optional] <p>TRUE === return {orig: string[], replace: string[]}
182
array</p>`
183
 
184
**Return:**
185
- `array <p>An array of replacements.</p>`
186
 
187
--------
188
 
189
#### clean(string $str, bool $normalize_whitespace, bool $keep_non_breaking_space, bool $normalize_msword, bool $remove_invisible_characters): string
190
<a href="#voku-php-readme-class-methods">↑</a>
191
Accepts a string and removes all non-UTF-8 characters from it + extras if needed.
192
 
193
**Parameters:**
194
- `string $str <p>The string to be sanitized.</p>`
195
- `bool $normalize_whitespace [optional] <p>Set to true, if you need to normalize the
196
whitespace.</p>`
197
- `bool $keep_non_breaking_space [optional] <p>Set to true, to keep non-breaking-spaces, in
198
combination with
199
$normalize_whitespace</p>`
200
- `bool $normalize_msword [optional] <p>Set to true, if you need to normalize MS Word chars
201
e.g.: "…"
202
=> "..."</p>`
203
- `bool $remove_invisible_characters [optional] <p>Set to false, if you not want to remove invisible
204
characters e.g.: "\0"</p>`
205
 
206
**Return:**
207
- `string <p>A clean UTF-8 string.</p>`
208
 
209
--------
210
 
211
#### getAllLanguages(): string[]
212
<a href="#voku-php-readme-class-methods">↑</a>
213
Get all languages from the constants "ASCII::.*LANGUAGE_CODE".
214
 
215
**Parameters:**
216
__nothing__
217
 
218
**Return:**
219
- `string[]`
220
 
221
--------
222
 
223
#### is_ascii(string $str): bool
224
<a href="#voku-php-readme-class-methods">↑</a>
225
Checks if a string is 7 bit ASCII.
226
 
227
EXAMPLE: <code>
228
ASCII::is_ascii('白'); // false
229
</code>
230
 
231
**Parameters:**
232
- `string $str <p>The string to check.</p>`
233
 
234
**Return:**
235
- `bool <p>
236
<strong>true</strong> if it is ASCII<br>
237
<strong>false</strong> otherwise
238
</p>`
239
 
240
--------
241
 
242
#### normalize_msword(string $str): string
243
<a href="#voku-php-readme-class-methods">↑</a>
244
Returns a string with smart quotes, ellipsis characters, and dashes from
245
Windows-1252 (commonly used in Word documents) replaced by their ASCII
246
equivalents.
247
 
248
EXAMPLE: <code>
249
ASCII::normalize_msword('„Abcdef…”'); // '"Abcdef..."'
250
</code>
251
 
252
**Parameters:**
253
- `string $str <p>The string to be normalized.</p>`
254
 
255
**Return:**
256
- `string <p>A string with normalized characters for commonly used chars in Word documents.</p>`
257
 
258
--------
259
 
260
#### normalize_whitespace(string $str, bool $keepNonBreakingSpace, bool $keepBidiUnicodeControls, bool $normalize_control_characters): string
261
<a href="#voku-php-readme-class-methods">↑</a>
262
Normalize the whitespace.
263
 
264
EXAMPLE: <code>
265
ASCII::normalize_whitespace("abc-\xc2\xa0-öäü-\xe2\x80\xaf-\xE2\x80\xAC", true); // "abc-\xc2\xa0-öäü- -"
266
</code>
267
 
268
**Parameters:**
269
- `string $str <p>The string to be normalized.</p>`
270
- `bool $keepNonBreakingSpace [optional] <p>Set to true, to keep non-breaking-spaces.</p>`
271
- `bool $keepBidiUnicodeControls [optional] <p>Set to true, to keep non-printable (for the web)
272
bidirectional text chars.</p>`
273
- `bool $normalize_control_characters [optional] <p>Set to true, to convert e.g. LINE-, PARAGRAPH-SEPARATOR with "\n" and LINE TABULATION with "\t".</p>`
274
 
275
**Return:**
276
- `string <p>A string with normalized whitespace.</p>`
277
 
278
--------
279
 
280
#### remove_invisible_characters(string $str, bool $url_encoded, string $replacement, bool $keep_basic_control_characters): string
281
<a href="#voku-php-readme-class-methods">↑</a>
282
Remove invisible characters from a string.
283
 
284
e.g.: This prevents sandwiching null characters between ascii characters, like Java\0script.
285
 
286
copy&past from https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Common.php
287
 
288
**Parameters:**
289
- `string $str`
290
- `bool $url_encoded`
291
- `string $replacement`
292
- `bool $keep_basic_control_characters`
293
 
294
**Return:**
295
- `string`
296
 
297
--------
298
 
299
#### to_ascii(string $str, string $language, bool $remove_unsupported_chars, bool $replace_extra_symbols, bool $use_transliterate, bool|null $replace_single_chars_only): string
300
<a href="#voku-php-readme-class-methods">↑</a>
301
Returns an ASCII version of the string. A set of non-ASCII characters are
302
replaced with their closest ASCII counterparts, and the rest are removed
303
by default. The language or locale of the source string can be supplied
304
for language-specific transliteration in any of the following formats:
305
en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
306
to "aeoeue" rather than "aou" as in other languages.
307
 
308
EXAMPLE: <code>
309
ASCII::to_ascii('�Düsseldorf�', 'en'); // Dusseldorf
310
</code>
311
 
312
**Parameters:**
313
- `string $str <p>The input string.</p>`
314
- `ASCII::* $language [optional] <p>Language of the source string.
315
(default is 'en') | ASCII::*_LANGUAGE_CODE</p>`
316
- `bool $remove_unsupported_chars [optional] <p>Whether or not to remove the
317
unsupported characters.</p>`
318
- `bool $replace_extra_symbols [optional]  <p>Add some more replacements e.g. "£" with " pound
319
".</p>`
320
- `bool $use_transliterate [optional]  <p>Use ASCII::to_transliterate() for unknown chars.</p>`
321
- `bool|null $replace_single_chars_only [optional]  <p>Single char replacement is better for the
322
performance, but some languages need to replace more then one char
323
at the same time. | NULL === auto-setting, depended on the
324
language</p>`
325
 
326
**Return:**
327
- `string <p>A string that contains only ASCII characters.</p>`
328
 
329
--------
330
 
331
#### to_ascii_remap(string $str1, string $str2): string[]
332
<a href="#voku-php-readme-class-methods">↑</a>
333
WARNING: This method will return broken characters and is only for special cases.
334
 
335
Convert two UTF-8 encoded string to a single-byte strings suitable for
336
functions that need the same string length after the conversion.
337
 
338
The function simply uses (and updates) a tailored dynamic encoding
339
(in/out map parameter) where non-ascii characters are remapped to
340
the range [128-255] in order of appearance.
341
 
342
**Parameters:**
343
- `string $str1`
344
- `string $str2`
345
 
346
**Return:**
347
- `string[]`
348
 
349
--------
350
 
351
#### to_filename(string $str, bool $use_transliterate, string $fallback_char): string
352
<a href="#voku-php-readme-class-methods">↑</a>
353
Convert given string to safe filename (and keep string case).
354
 
355
EXAMPLE: <code>
356
ASCII::to_filename('שדגשדג.png', true)); // 'shdgshdg.png'
357
</code>
358
 
359
**Parameters:**
360
- `string $str`
361
- `bool $use_transliterate <p>ASCII::to_transliterate() is used by default - unsafe characters are
362
simply replaced with hyphen otherwise.</p>`
363
- `string $fallback_char`
364
 
365
**Return:**
366
- `string <p>A string that contains only safe characters for a filename.</p>`
367
 
368
--------
369
 
370
#### to_slugify(string $str, string $separator, string $language, string[] $replacements, bool $replace_extra_symbols, bool $use_str_to_lower, bool $use_transliterate): string
371
<a href="#voku-php-readme-class-methods">↑</a>
372
Converts the string into an URL slug. This includes replacing non-ASCII
373
characters with their closest ASCII equivalents, removing remaining
374
non-ASCII and non-alphanumeric characters, and replacing whitespace with
375
$separator. The separator defaults to a single dash, and the string
376
is also converted to lowercase. The language of the source string can
377
also be supplied for language-specific transliteration.
378
 
379
**Parameters:**
380
- `string $str`
381
- `string $separator [optional] <p>The string used to replace whitespace.</p>`
382
- `ASCII::* $language [optional] <p>Language of the source string.
383
(default is 'en') | ASCII::*_LANGUAGE_CODE</p>`
384
- `array<string, string> $replacements [optional] <p>A map of replaceable strings.</p>`
385
- `bool $replace_extra_symbols [optional]  <p>Add some more replacements e.g. "£" with "
386
pound ".</p>`
387
- `bool $use_str_to_lower [optional] <p>Use "string to lower" for the input.</p>`
388
- `bool $use_transliterate [optional]  <p>Use ASCII::to_transliterate() for unknown
389
chars.</p>`
390
 
391
**Return:**
392
- `string <p>A string that has been converted to an URL slug.</p>`
393
 
394
--------
395
 
396
#### to_transliterate(string $str, string|null $unknown, bool $strict): string
397
<a href="#voku-php-readme-class-methods">↑</a>
398
Returns an ASCII version of the string. A set of non-ASCII characters are
399
replaced with their closest ASCII counterparts, and the rest are removed
400
unless instructed otherwise.
401
 
402
EXAMPLE: <code>
403
ASCII::to_transliterate('déjà σσς iıii'); // 'deja sss iiii'
404
</code>
405
 
406
**Parameters:**
407
- `string $str <p>The input string.</p>`
408
- `string|null $unknown [optional] <p>Character use if character unknown. (default is '?')
409
But you can also use NULL to keep the unknown chars.</p>`
410
- `bool $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl`
411
 
412
**Return:**
413
- `string <p>A String that contains only ASCII characters.</p>`
414
 
415
--------
416
 
417
 
418
 
419
## Unit Test
420
 
421
1) [Composer](https://getcomposer.org) is a prerequisite for running the tests.
422
 
423
```
424
composer install
425
```
426
 
427
2) The tests can be executed by running this command from the root directory:
428
 
429
```bash
430
./vendor/bin/phpunit
431
```
432
 
433
### Support
434
 
435
For support and donations please visit [Github](https://github.com/voku/portable-ascii/) | [Issues](https://github.com/voku/portable-ascii/issues) | [PayPal](https://paypal.me/moelleken) | [Patreon](https://www.patreon.com/voku).
436
 
437
For status updates and release announcements please visit [Releases](https://github.com/voku/portable-ascii/releases) | [Twitter](https://twitter.com/suckup_de) | [Patreon](https://www.patreon.com/voku/posts).
438
 
439
For professional support please contact [me](https://about.me/voku).
440
 
441
### Thanks
442
 
443
- Thanks to [GitHub](https://github.com) (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
444
- Thanks to [IntelliJ](https://www.jetbrains.com) as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
445
- Thanks to [Travis CI](https://travis-ci.com/) for being the most awesome, easiest continous integration tool out there!
446
- Thanks to [StyleCI](https://styleci.io/) for the simple but powerful code style check.
447
- Thanks to [PHPStan](https://github.com/phpstan/phpstan) && [Psalm](https://github.com/vimeo/psalm) for really great Static analysis tools and for discover bugs in the code!
448
 
449
### License and Copyright
450
 
451
Released under the MIT License - see `LICENSE.txt` for details.