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   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-09-07 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 14 This week: 14All time: 9,680 This week: 21Up
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

  Performance   Level  
Innovation award
Innovation award
Nominee: 8x

 

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:14
This week:14
All time:9,680
This week:21Up