Skip to content

Commit 21aa79c

Browse files
committed
Add utility function to sanitize file and folder name
Signed-off-by: Riddhesh Sanghvi <[email protected]>
1 parent 81c3c4e commit 21aa79c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

php/utils.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,3 +1761,21 @@ function get_value_if_flag_isset( $assoc_args, $flag, $supported_flag_values = [
17611761
}
17621762
return $value;
17631763
}
1764+
1765+
/**
1766+
* Function to sanitize and remove illegal characters for folder and filename.
1767+
*
1768+
* @param string $input_name Input name to be sanitized.
1769+
*
1770+
* @return string Sanitized name valid for file/folder creation.
1771+
*/
1772+
function sanitize_file_folder_name( $input_name ) {
1773+
1774+
// Remove Illegal Chars for folder and filename.
1775+
$output = preg_replace('/[\"\*\/\:\<\>\?\'\|]+/', ' ', $input_name);
1776+
1777+
// Replace Spaces with dashes.
1778+
$output = str_replace(' ', '-', $output);
1779+
1780+
return trim( $output, '-' );
1781+
}

0 commit comments

Comments
 (0)