PHP Classes

This class is overly complex and will miss some special chara...

Recommend this page to a friend!

      PHP Sanitize Filename String  >  All threads  >  This class is overly complex and...  >  (Un) Subscribe thread alerts  
Subject:This class is overly complex and...
Summary:Package rating comment
Messages:1
Author:John Conde
Date:2020-07-26 16:56:49
 

John Conde rated this package as follows:

Utility: Insufficient
Consistency: Sufficient
Documentation: Sufficient

  1. This class is overly complex and...   Reply   Report abuse  
Picture of John Conde John Conde - 2020-07-26 16:56:49
This class is overly complex and will miss some special characters. Doing a 1:1 replace leaves the opportunity to miss some characters and that certainly exists in this class meaning this class is defective and not ready for a production environment.

Example code demonstrating the issues:

$strings = [
'à, è, ì, ò, ù',
'À, È, Ì, Ò, Ù',
'á, é, í, ó, ú, ý',
'Á, É, Í, Ó, Ú, Ý',
'â, ê, î, ô, û',
'Â, Ê, Î, Ô, Û',
'ã, ñ, õ',
'Ã, Ñ, Õ',
'ä, ë, ï, ö, ü, ÿ',
'Ä, Ë, Ï, Ö, Ü, Ÿ',
'å, Å',
'æ, Æ',
'œ, Œ',
'ç, Ç',
'ð, Ð',
'ø, Ø',
'¿',
'¡',
'ß',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz0123456789~!@#$%&*()-=+{};:"<>,.?/\'',
];
foreach ($strings as $string) {
printf('%s%s', SanitizeString::clean($string, false, true), PHP_EOL);
}

OUTPUT:

a, e, i, o, u
A, E, I, O, U
a, e, i, o, u, y
A, E, I, O, U, Y
a, e, i, o, u
A, E, I, O, U
a, n, o
A, N, O
a, e, i, o, ü, y
A, E, I, O, U, Ÿ
a, A
a, A
œ, Œ
c, C
o, Ð
o, O
¿
¡
Ss
ABCDEFGHIJKLMNOPQRSTUVWXYZ\_`abcdefghijklmnopqrstuvwxyz0123456789-=+;:",.z'

This alternative class is more concise and effective.

(Note: this example considers a " " (space character) to be a valid special character. This could be expanded upon to make that configurable.)

class SanitizeString
{
/**
* @param string $newString
* @param bool $special defaults to false
* @return string
**/
private static function sanitize($newString, $special = false)
{
$str = htmlentities($newString, ENT_QUOTES, 'UTF-8');
$str = preg_replace('/&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);/i', '$1', $str);
$str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
if ($special) {
$str = preg_replace('/[^0-9a-z ]+/i', '', $str);
}
return trim($str);
}


/**
* Replace foreign and special characters on a filename or string
* @param string $string
* @param bool $isFileName
* @param bool $special
* @return bool|string
**/
public static function clean($string, $isFileName = false, $special = false)
{
$newName = self::sanitize($string, $special);
if ($isFileName) {
return rename($string, $newName);
}
return $newName;
}
}