PHP Sanitize Filename String: Change file name to use only ASCII characters

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2020-08-03 (29 days ago) RSS 2.0 feedNot enough user ratingsTotal: 151 This week: 67All time: 8,831 This week: 3Up
Version License PHP version Categories
sanitize_string 1.3The PHP License5.6PHP 5, Files and Folders, Text proces...
Description Author

This class can change file name to use only ASCII characters.

It can take a given string of a file and replaces non-ASCII characters by other ASCII characters that are more similar to the original characters.

The class can also rename a existing file named with the given input string and rename it to a new file name that has only the ASCII characters after that file name string has been sanitized.

Innovation Award
PHP Programming Innovation award nominee
July 2020
Number 4
When applications need to create files, it is convenient to only use ASCII characters in the file names to avoid portability problems when those files may be used by people that have computers or mobile devices that use different operating systems.

This package can not only replace non-ASCII characters by similar ASCII character versions in a string, but it can also rename a file that has non-ASCII characters to use only similar ASCII characters.

Manuel Lemos
Picture of Julio Vergara
  Performance   Level  
Name: Julio Vergara <contact>
Classes: 1 package by
Country: United States United States
Innovation award
Innovation award
Nominee: 1x

Details

Sanitize String Class

Version Total Downloads Latest Unstable Version License

Sometimes you may want to clean a filename or string from foreign languages characters; with this class you can do both.

Usage

Install via composer:

composer require vgd/sanitize_string

or

require('SanitizeString.php');


The class uses two boolean parameters after the string/filename: 

The first one (isFileName) is used to tell the class that you are trying to clean a file name.

The second one (special) must be used if you would like to remove special characters as well from a 
string or a file name.

In order to clean a string:

To remove foreign characters only:

echo SanitizeString::clean('Your string here');


To remove foreign AND special characters:

echo SanitizeString::clean('Your string here', false, true);

In order to clean a filename:

To remove foreign characters only:

SanitizeString::clean('Your filename here', true, false);


To remove foreign AND special characters:

SanitizeString::clean('Your filename here', true, true);


You can use absolute or relative paths for the file you want to rename.

Final Note

The main purpose of using associative arrays and not other techniques as regular 
expressions and preg_match etc. was to give the user the possibility of add or remove
characters depending of their needs.

Lets say that you have a spanish text and you want to remove all the characters but vowels 
with tilde and ñ characters; you can modify the arrays to achieve that.

  Files folder image Files  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. MIT License
Accessible without login Plain text file README.md Doc. Documentation
Plain text file SanitizeString.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:151
This week:67
All time:8,831
This week:3Up
User Comments (1)
 

For more information send a message to info at phpclasses dot org.