mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-13 01:51:50 +01:00
[Miscellaneous] [Regex] Use simpler & stronger pattern to match name of file
This commit is contained in:
@@ -200,13 +200,13 @@ class Miscellaneous
|
||||
* @param string $path A path that contains file name
|
||||
* @return string
|
||||
*/
|
||||
public static function getFileNameFromPath($path)
|
||||
public static function getFileNameFromPath(string $path): string
|
||||
{
|
||||
$matches = [];
|
||||
$pattern = sprintf('|([^\%s.]+\.[A-Za-z0-9.]+)$|', DIRECTORY_SEPARATOR);
|
||||
$pattern = Regex::getFileNamePattern();
|
||||
|
||||
if ((bool)preg_match($pattern, $path, $matches)) {
|
||||
return $matches[1];
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
return '';
|
||||
@@ -229,7 +229,7 @@ class Miscellaneous
|
||||
* Let's clear name of file
|
||||
*
|
||||
* Attention.
|
||||
* The name without extension may be cleared / urlized only to avoid incorrect name by replacing "." with "-".
|
||||
* The name without extension should be cleared to avoid incorrect name by replacing "." with "-".
|
||||
*/
|
||||
$withoutExtension = Urlizer::urlize($withoutExtension);
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ class Regex
|
||||
'letterOrDigit' => '/[a-zA-Z0-9]+/',
|
||||
'htmlEntity' => '/&[a-z0-9]+;/',
|
||||
'htmlAttribute' => '/([\w-]+)="([\w -]+)"/',
|
||||
'fileName' => '/.+\.\w+$/',
|
||||
'fileName' => '/[\w.\- +=!@$&()?]+\.\w+$/', // e.g. "this-1_2 3 & my! 4+file.jpg"
|
||||
'isQuoted' => '/^[\'"]{1}.+[\'"]{1}$/',
|
||||
'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/',
|
||||
'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/',
|
||||
@@ -625,7 +625,7 @@ class Regex
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getFileNamePattern()
|
||||
public static function getFileNamePattern(): string
|
||||
{
|
||||
return self::$patterns['fileName'];
|
||||
}
|
||||
@@ -637,7 +637,7 @@ class Regex
|
||||
* @param string $fileName Name of file to check. It may be path of file also.
|
||||
* @return bool
|
||||
*/
|
||||
public static function isFileName($fileName)
|
||||
public static function isFileName(string $fileName): bool
|
||||
{
|
||||
$pattern = self::getFileNamePattern();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user