PHP Classes
elePHPant
Icontem

portable ascii: Manipulate text strings without special extensions

Recommend this page to a friend!
  Info   View files Documentation   View files View files (211)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-09-07 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 75 All time: 9,470 This week: 328Up
Version License PHP version Categories
portable-ascii 1.2.0MIT/X Consortium ...7Text processing, PHP 7
Description Author

This package can manipulate text strings without special extensions.

It provides several text manipulation functions that are based on pure PHP code, so they do not use extensions like mb_string or iconv. Currently it can:

- Convert strings to only use ASCII characters by mapping characters that are similar but without accents and cedillas.
- Get an array for replacing characters in a string to ASCII for different languages
- Check of a string only contains ASCII characters
- Normalize a string with special characters created by Microsoft Word.
- Normalize a string with white space characters
- Convert strings of file names to use only ASCII characters
- Transliterate text strings use only equivalent ASCII characters

Innovation Award
PHP Programming Innovation award winner
September 2019
Winner


Prize: One official elePHPant Plush Mascott
Some applications need to use text that must use only ASCII characters, thus not have any letters with accents or cedillas for instance.

This package can manipulate text strings with any text so it can convert them to ASCII based text without any PHP extensions, like mb_string or iconv.

Manuel Lemos
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 20 packages by
Country: Germany Germany
Age: 32
All time rank: 87256 in Germany Germany
Week rank: 14 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 9x

Winner: 1x

 

Details

Build Status Build status Coverage Status Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

? Portable ASCII

Description

It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your server.

The benefit of Portable ASCII is that it is easy to use, easy to bundle.

The project based on ... + Portable UTF-8 work (https://github.com/voku/portable-utf8) + Daniel St. Jules's work (https://github.com/danielstjules/Stringy) + Johnny Broadway's work (https://github.com/jbroadway/urlify) + and many cherry-picks from "github"-gists and "Stack Overflow"-snippets ...

Index

Alternative

If you like a more Object Oriented Way to edit strings, then you can take a look at voku/Stringy, it's a fork of "danielstjules/Stringy" but it used the "Portable ASCII"-Class and some extra methods.

// Portable ASCII
use voku\helper\ASCII;
ASCII::to_transliterate('déjà ??? i?ii'); // 'deja sss iiii'

// voku/Stringy
use Stringy\Stringy as S;
$stringy = S::create('déjà ??? i?ii');
$stringy->toTransliterate();              // 'deja sss iiii'

Install "Portable ASCII" via "composer require"

composer require voku/portable-ascii

Why Portable ASCII?[]()

I need ASCII char handling in different classes and before I added this functions into "Portable UTF-8", but this repo is more modular and portable, because it has no dependencies.

Requirements and Recommendations

  • 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.
  • PHP 7.0 is the minimum requirement

Usage

Example: ASCII::to_ascii()

  echo ASCII::to_ascii('?Düsseldorf?', 'de');
  
  // will output
  // Duesseldorf

  echo ASCII::to_ascii('?Düsseldorf?', 'en');
  
  // will output
  // Dusseldorf

Portable ASCII | API

The API from the "ASCII"-Class is written as small static methods.

Class methods

charsArray(bool $withExtras = false): array

Returns an replacement array for ASCII methods.

$array = ASCII::charsArray();

var_dump($array['ru']['?']); // 'b'

charsArrayWithMultiLanguageValues(bool $withExtras = false): array

Returns an replacement array for ASCII methods with a mix of multiple languages.

$array = ASCII::charsArrayWithMultiLanguageValues();

var_dump($array['b']); // ['?', '?', '?', '?', '?']

charsArrayWithOneLanguage(string $language = 'en', bool $withExtras = false): array {
      

Returns an replacement array for ASCII methods with one language.

For example, German will map 'ä' to 'ae', while other languages will simply return e.g. 'a'.

$array = ASCII::charsArrayWithOneLanguage('ru');

$tmpKey = \array_search('yo', $array['replace']);
echo $array['orig'][$tmpKey]; // '?'

charsArrayWithSingleLanguageValues(bool $withExtras = false): array
      

Returns an replacement array for ASCII methods with multiple languages.

$array = ASCII::charsArrayWithSingleLanguageValues();

$tmpKey = \array_search('hnaik', $array['replace']);
echo $array['orig'][$tmpKey]; // '?'

is_ascii(string $str) : bool

Checks if a string is 7 bit ASCII.

ASCII::is_ascii('?'); // false

normalize_msword(string $str) : string

Normalize some MS Word special characters.

ASCII::normalize_msword('?Abcdef??'); // '"Abcdef..."'

normalize_whitespace(string $str, bool $keepNonBreakingSpace = false, bool $keepBidiUnicodeControls = false) : string

Normalize the whitespace.

ASCII::normalize_whitespace("abc-\xc2\xa0-öäü-\xe2\x80\xaf-\xE2\x80\xAC", true); // "abc-\xc2\xa0-öäü- -"

to_ascii(string $str, string $language = 'en', bool $removeUnsupported = true): string

Convert a string into language specific ASCII.

ASCII::to_ascii('?Düsseldorf?', 'en'); // Dusseldorf

to_filename(string $str, bool $use_transliterate = false, string $fallback_char = '-'): string

Convert given string to safe filename (and keep string case).

ASCII::to_filename('??????.png', true)); // 'shdgshdg.png'

to_transliterate(string $str, string $unknown = '?', bool $strict) : string

Convert a string into ASCII.

ASCII::to_transliterate('déjà ??? i?ii'); // 'deja sss iiii'

Unit Test

1) Composer is a prerequisite for running the tests.

composer install

2) The tests can be executed by running this command from the root directory:

./vendor/bin/phpunit

Support

For support and donations please visit Github | Issues | PayPal | Patreon.

For status updates and release announcements please visit Releases | Twitter | Patreon.

For professional support please contact me.

Thanks

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
  • Thanks to StyleCI for the simple but powerfull code style check.
  • Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!

License and Copyright

Released under the MIT License - see LICENSE.txt for details.

  Files folder image Files  
File Role Description
Files folder image.github (4 files)
Files folder imagesrc (1 directory)
Files folder imagetests (4 files, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file appveyor.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file circle.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file phpcs.php_cs Example Example script
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data
  Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data
  Accessible without login Plain text file PULL_REQUEST_TEMPLATE.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imagevoku (1 directory)

  Files folder image Files  /  src  /  voku  
File Role Description
Files folder imagehelper (1 file, 1 directory)

  Files folder image Files  /  src  /  voku  /  helper  
File Role Description
Files folder imagedata (187 files)
  Accessible without login Plain text file ASCII.php Class Class source

  Files folder image Files  /  src  /  voku  /  helper  /  data  
File Role Description
  Accessible without login Plain text file ascii_by_languages.php Aux. Auxiliary script
  Accessible without login Plain text file ascii_extras_by_languages.php Aux. Auxiliary script
  Accessible without login Plain text file ascii_ord.php Aux. Auxiliary script
  Accessible without login Plain text file x00.php Aux. Auxiliary script
  Accessible without login Plain text file x01.php Aux. Auxiliary script
  Accessible without login Plain text file x02.php Aux. Auxiliary script
  Accessible without login Plain text file x03.php Aux. Auxiliary script
  Accessible without login Plain text file x04.php Aux. Auxiliary script
  Accessible without login Plain text file x05.php Aux. Auxiliary script
  Accessible without login Plain text file x06.php Aux. Auxiliary script
  Accessible without login Plain text file x07.php Aux. Auxiliary script
  Accessible without login Plain text file x09.php Aux. Auxiliary script
  Accessible without login Plain text file x0a.php Aux. Auxiliary script
  Accessible without login Plain text file x0b.php Aux. Auxiliary script
  Accessible without login Plain text file x0c.php Aux. Auxiliary script
  Accessible without login Plain text file x0d.php Aux. Auxiliary script
  Accessible without login Plain text file x0e.php Aux. Auxiliary script
  Accessible without login Plain text file x0f.php Aux. Auxiliary script
  Accessible without login Plain text file x10.php Aux. Auxiliary script
  Accessible without login Plain text file x11.php Aux. Auxiliary script
  Accessible without login Plain text file x12.php Aux. Auxiliary script
  Accessible without login Plain text file x13.php Aux. Auxiliary script
  Accessible without login Plain text file x14.php Aux. Auxiliary script
  Accessible without login Plain text file x15.php Aux. Auxiliary script
  Accessible without login Plain text file x16.php Aux. Auxiliary script
  Accessible without login Plain text file x17.php Aux. Auxiliary script
  Accessible without login Plain text file x18.php Aux. Auxiliary script
  Accessible without login Plain text file x1d4.php Aux. Auxiliary script
  Accessible without login Plain text file x1d5.php Aux. Auxiliary script
  Accessible without login Plain text file x1d6.php Aux. Auxiliary script
  Accessible without login Plain text file x1e.php Aux. Auxiliary script
  Accessible without login Plain text file x1f.php Aux. Auxiliary script
  Accessible without login Plain text file x1f1.php Aux. Auxiliary script
  Accessible without login Plain text file x20.php Aux. Auxiliary script
  Accessible without login Plain text file x21.php Aux. Auxiliary script
  Accessible without login Plain text file x22.php Aux. Auxiliary script
  Accessible without login Plain text file x23.php Aux. Auxiliary script
  Accessible without login Plain text file x24.php Aux. Auxiliary script
  Accessible without login Plain text file x25.php Aux. Auxiliary script
  Accessible without login Plain text file x26.php Aux. Auxiliary script
  Accessible without login Plain text file x27.php Aux. Auxiliary script
  Accessible without login Plain text file x28.php Aux. Auxiliary script
  Accessible without login Plain text file x2e.php Aux. Auxiliary script
  Accessible without login Plain text file x2f.php Aux. Auxiliary script
  Accessible without login Plain text file x30.php Aux. Auxiliary script
  Accessible without login Plain text file x31.php Aux. Auxiliary script
  Accessible without login Plain text file x32.php Aux. Auxiliary script
  Accessible without login Plain text file x33.php Aux. Auxiliary script
  Accessible without login Plain text file x4d.php Aux. Auxiliary script
  Accessible without login Plain text file x4e.php Aux. Auxiliary script
  Accessible without login Plain text file x4f.php Aux. Auxiliary script
  Accessible without login Plain text file x50.php Aux. Auxiliary script
  Accessible without login Plain text file x51.php Aux. Auxiliary script
  Accessible without login Plain text file x52.php Aux. Auxiliary script
  Accessible without login Plain text file x53.php Aux. Auxiliary script
  Accessible without login Plain text file x54.php Aux. Auxiliary script
  Accessible without login Plain text file x55.php Aux. Auxiliary script
  Accessible without login Plain text file x56.php Aux. Auxiliary script
  Accessible without login Plain text file x57.php Aux. Auxiliary script
  Accessible without login Plain text file x58.php Aux. Auxiliary script
  Accessible without login Plain text file x59.php Aux. Auxiliary script
  Accessible without login Plain text file x5a.php Aux. Auxiliary script
  Accessible without login Plain text file x5b.php Aux. Auxiliary script
  Accessible without login Plain text file x5c.php Aux. Auxiliary script
  Accessible without login Plain text file x5d.php Aux. Auxiliary script
  Accessible without login Plain text file x5e.php Aux. Auxiliary script
  Accessible without login Plain text file x5f.php Aux. Auxiliary script
  Accessible without login Plain text file x60.php Aux. Auxiliary script
  Accessible without login Plain text file x61.php Aux. Auxiliary script
  Accessible without login Plain text file x62.php Aux. Auxiliary script
  Accessible without login Plain text file x63.php Aux. Auxiliary script
  Accessible without login Plain text file x64.php Aux. Auxiliary script
  Accessible without login Plain text file x65.php Aux. Auxiliary script
  Accessible without login Plain text file x66.php Aux. Auxiliary script
  Accessible without login Plain text file x67.php Aux. Auxiliary script
  Accessible without login Plain text file x68.php Aux. Auxiliary script
  Accessible without login Plain text file x69.php Aux. Auxiliary script
  Accessible without login Plain text file x6a.php Aux. Auxiliary script
  Accessible without login Plain text file x6b.php Aux. Auxiliary script
  Accessible without login Plain text file x6c.php Aux. Auxiliary script
  Accessible without login Plain text file x6d.php Aux. Auxiliary script
  Accessible without login Plain text file x6e.php Aux. Auxiliary script
  Accessible without login Plain text file x6f.php Aux. Auxiliary script
  Accessible without login Plain text file x70.php Aux. Auxiliary script
  Accessible without login Plain text file x71.php Aux. Auxiliary script
  Accessible without login Plain text file x72.php Aux. Auxiliary script
  Accessible without login Plain text file x73.php Aux. Auxiliary script
  Accessible without login Plain text file x74.php Aux. Auxiliary script
  Accessible without login Plain text file x75.php Aux. Auxiliary script
  Accessible without login Plain text file x76.php Aux. Auxiliary script
  Accessible without login Plain text file x77.php Aux. Auxiliary script
  Accessible without login Plain text file x78.php Aux. Auxiliary script
  Accessible without login Plain text file x79.php Aux. Auxiliary script
  Accessible without login Plain text file x7a.php Aux. Auxiliary script
  Accessible without login Plain text file x7b.php Aux. Auxiliary script
  Accessible without login Plain text file x7c.php Aux. Auxiliary script
  Accessible without login Plain text file x7d.php Aux. Auxiliary script
  Accessible without login Plain text file x7e.php Aux. Auxiliary script
  Accessible without login Plain text file x7f.php Aux. Auxiliary script
  Accessible without login Plain text file x80.php Aux. Auxiliary script
  Accessible without login Plain text file x81.php Aux. Auxiliary script
  Accessible without login Plain text file x82.php Aux. Auxiliary script
  Accessible without login Plain text file x83.php Aux. Auxiliary script
  Accessible without login Plain text file x84.php Aux. Auxiliary script
  Accessible without login Plain text file x85.php Aux. Auxiliary script
  Accessible without login Plain text file x86.php Aux. Auxiliary script
  Accessible without login Plain text file x87.php Aux. Auxiliary script
  Accessible without login Plain text file x88.php Aux. Auxiliary script
  Accessible without login Plain text file x89.php Aux. Auxiliary script
  Accessible without login Plain text file x8a.php Aux. Auxiliary script
  Accessible without login Plain text file x8b.php Aux. Auxiliary script
  Accessible without login Plain text file x8c.php Aux. Auxiliary script
  Accessible without login Plain text file x8d.php Aux. Auxiliary script
  Accessible without login Plain text file x8e.php Aux. Auxiliary script
  Accessible without login Plain text file x8f.php Aux. Auxiliary script
  Accessible without login Plain text file x90.php Aux. Auxiliary script
  Accessible without login Plain text file x91.php Aux. Auxiliary script
  Accessible without login Plain text file x92.php Aux. Auxiliary script
  Accessible without login Plain text file x93.php Aux. Auxiliary script
  Accessible without login Plain text file x94.php Aux. Auxiliary script
  Accessible without login Plain text file x95.php Aux. Auxiliary script
  Accessible without login Plain text file x96.php Aux. Auxiliary script
  Accessible without login Plain text file x97.php Aux. Auxiliary script
  Accessible without login Plain text file x98.php Aux. Auxiliary script
  Accessible without login Plain text file x99.php Aux. Auxiliary script
  Accessible without login Plain text file x9a.php Aux. Auxiliary script
  Accessible without login Plain text file x9b.php Aux. Auxiliary script
  Accessible without login Plain text file x9c.php Aux. Auxiliary script
  Accessible without login Plain text file x9d.php Aux. Auxiliary script
  Accessible without login Plain text file x9e.php Aux. Auxiliary script
  Accessible without login Plain text file x9f.php Aux. Auxiliary script
  Accessible without login Plain text file xa0.php Aux. Auxiliary script
  Accessible without login Plain text file xa1.php Aux. Auxiliary script
  Accessible without login Plain text file xa2.php Aux. Auxiliary script
  Accessible without login Plain text file xa3.php Aux. Auxiliary script
  Accessible without login Plain text file xa4.php Aux. Auxiliary script
  Accessible without login Plain text file xac.php Aux. Auxiliary script
  Accessible without login Plain text file xad.php Aux. Auxiliary script
  Accessible without login Plain text file xae.php Aux. Auxiliary script
  Accessible without login Plain text file xaf.php Aux. Auxiliary script
  Accessible without login Plain text file xb0.php Aux. Auxiliary script
  Accessible without login Plain text file xb1.php Aux. Auxiliary script
  Accessible without login Plain text file xb2.php Aux. Auxiliary script
  Accessible without login Plain text file xb3.php Aux. Auxiliary script
  Accessible without login Plain text file xb4.php Aux. Auxiliary script
  Accessible without login Plain text file xb5.php Aux. Auxiliary script
  Accessible without login Plain text file xb6.php Aux. Auxiliary script
  Accessible without login Plain text file xb7.php Aux. Auxiliary script
  Accessible without login Plain text file xb8.php Aux. Auxiliary script
  Accessible without login Plain text file xb9.php Aux. Auxiliary script
  Accessible without login Plain text file xba.php Aux. Auxiliary script
  Accessible without login Plain text file xbb.php Aux. Auxiliary script
  Accessible without login Plain text file xbc.php Aux. Auxiliary script
  Accessible without login Plain text file xbd.php Aux. Auxiliary script
  Accessible without login Plain text file xbe.php Aux. Auxiliary script
  Accessible without login Plain text file xbf.php Aux. Auxiliary script
  Accessible without login Plain text file xc0.php Aux. Auxiliary script
  Accessible without login Plain text file xc1.php Aux. Auxiliary script
  Accessible without login Plain text file xc2.php Aux. Auxiliary script
  Accessible without login Plain text file xc3.php Aux. Auxiliary script
  Accessible without login Plain text file xc4.php Aux. Auxiliary script
  Accessible without login Plain text file xc5.php Aux. Auxiliary script
  Accessible without login Plain text file xc6.php Aux. Auxiliary script
  Accessible without login Plain text file xc7.php Aux. Auxiliary script
  Accessible without login Plain text file xc8.php Aux. Auxiliary script
  Accessible without login Plain text file xc9.php Aux. Auxiliary script
  Accessible without login Plain text file xca.php Aux. Auxiliary script
  Accessible without login Plain text file xcb.php Aux. Auxiliary script
  Accessible without login Plain text file xcc.php Aux. Auxiliary script
  Accessible without login Plain text file xcd.php Aux. Auxiliary script
  Accessible without login Plain text file xce.php Aux. Auxiliary script
  Accessible without login Plain text file xcf.php Aux. Auxiliary script
  Accessible without login Plain text file xd0.php Aux. Auxiliary script
  Accessible without login Plain text file xd1.php Aux. Auxiliary script
  Accessible without login Plain text file xd2.php Aux. Auxiliary script
  Accessible without login Plain text file xd3.php Aux. Auxiliary script
  Accessible without login Plain text file xd4.php Aux. Auxiliary script
  Accessible without login Plain text file xd5.php Aux. Auxiliary script
  Accessible without login Plain text file xd6.php Aux. Auxiliary script
  Accessible without login Plain text file xd7.php Aux. Auxiliary script
  Accessible without login Plain text file xf9.php Aux. Auxiliary script
  Accessible without login Plain text file xfa.php Aux. Auxiliary script
  Accessible without login Plain text file xfb.php Aux. Auxiliary script
  Accessible without login Plain text file xfc.php Aux. Auxiliary script
  Accessible without login Plain text file xfd.php Aux. Auxiliary script
  Accessible without login Plain text file xfe.php Aux. Auxiliary script
  Accessible without login Plain text file xff.php Aux. Auxiliary script

  Files folder image Files  /  tests  
File Role Description
Files folder imagefixtures (1 file)
  Accessible without login Plain text file AsciiGlobalTest.php Class Class source
  Accessible without login Plain text file AsciiTest.php Class Class source
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Accessible without login Plain text file TransliterateTest.php Class Class source

  Files folder image Files  /  tests  /  fixtures  
File Role Description
  Accessible without login Plain text file sample-ascii-chart.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:75
This week:0
All time:9,470
This week:328Up
User Comments (1)
Thats a very good class, great job ;-)
1 month ago (José Filipe Lopes Santos)
80%StarStarStarStarStar