Skip to content

Commit 2b339e0

Browse files
committed
Add option for '/' in sanitization
Signed-off-by: Riddhesh Sanghvi <[email protected]>
1 parent d343842 commit 2b339e0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

php/utils.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,18 +1765,22 @@ function get_value_if_flag_isset( $assoc_args, $flag, $supported_flag_values = [
17651765
/**
17661766
* Function to sanitize and remove illegal characters for folder and filename.
17671767
*
1768-
* @param string $input_name Input name to be sanitized.
1769-
* @param bool $strict Do strict replacement, i.e, remove all special characters except `-` and `_`.
1768+
* @param string $input_name Input name to be sanitized.
1769+
* @param bool $strict Do strict replacement, i.e, remove all special characters except `-` and `_`.
1770+
* @param bool $remove_forward_slashes Wether to remove `/` or not from the input.
17701771
*
17711772
* @return string Sanitized name valid for file/folder creation.
17721773
*/
1773-
function sanitize_file_folder_name( $input_name, $strict = true ) {
1774+
function sanitize_file_folder_name( $input_name, $strict = true, $remove_forward_slashes = false ) {
1775+
1776+
$expression = $remove_forward_slashes ? '/[\"\*\/\:\<\>\?\'\|]+/' : '/[\"\*\:\<\>\?\'\|]+/';
17741777

17751778
// Remove Illegal Chars for folder and filename.
1776-
$output = preg_replace( '/[\"\*\/\:\<\>\?\'\|]+/', ' ', $input_name );
1779+
$output = preg_replace( $expression, '', $input_name );
17771780

17781781
if ( $strict ) {
1779-
$output = preg_replace( '/[^A-Za-z0-9\-\_]/', '', $output );
1782+
// Remove all special characters except `-`, `_` and `/`.
1783+
$output = preg_replace( '/[^A-Za-z0-9\-_\/]/', '', $output );
17801784
}
17811785
// Replace Spaces with dashes.
17821786
$output = str_replace( ' ', '-', $output );

0 commit comments

Comments
 (0)