sanitize_file_name()清理文件名,并把空白替换成破折号 -

目录

描述

译文

过滤文件名中的特定字符串。

删除所有字符串,只保留以空格、破折号以及时间隔开的字母数字式字符串,将所有字符串转换为小写状态。所有空格和下划线都被转换为破折号。将多个破折号转换为单一破折号。删除名称以破折号结尾的文件。

原文

清理文件 ,并把 白替换成破 号 -

删除在一些操作系统的文件名中 非法的特色字 ,并且 空格和连续的  转换成  破折 ,去掉文件名前后的点好,破折豪和下划线。

sanitize_file_name() 描述

用法

<?php sanitize_file_name$name ?>

sanitize_file_name() 用法

参数

$name

(string) (必填) 要清理的文件名

默认值: None

sanitize_file_name() 参数

返回值

(string) 

清理 后的文件名

sanitize_file_name() 返回值

历史

添加于 版本: 2.1

sanitize_file_name() 历史

源文件

sanitize_file_name() 函数的代码位于 wp-includes/formatting.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Sanitizes a filename, replacing whitespace with dashes.
 *
 * Removes special characters that are illegal in filenames on certain
 * operating systems and special characters requiring special escaping
 * to manipulate at the command line. Replaces spaces and consecutive
 * dashes with a single dash. Trims period, dash and underscore from beginning
 * and end of filename.
 *
 * @since 2.1.0
 *
 * @param string $filename The filename to be sanitized
 * @return string The sanitized filename
 */
function sanitize_file_name( $filename ) {
	$filename_raw = $filename;
	$special_chars = array("?", "[", "]", "/", "", "=", "<", "="">", ":", ";", ",", "'", """, "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
	/**
	 * Filter the list of characters to remove from a filename.
	 *
	 * @since 2.8.0
	 *
	 * @param array  $special_chars Characters to remove.
	 * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
	 */
	$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
	$filename = preg_replace( "#x{00a0}#siu", ' ', $filename );
	$filename = str_replace( $special_chars, '', $filename );
	$filename = str_replace( array( '%20', '+' ), '-', $filename );
	$filename = preg_replace( '/[
	 -]+/', '-', $filename );
	$filename = trim( $filename, '.-_' );

	// Split the filename into a base and extension[s]
	$parts = explode('.', $filename);

	// Return if only one extension
	if ( count( $parts ) <= 2="" )="" {="" *="" *="" filter="" a="" sanitized="" filename="" string.="" *="" *="" @since="" 2.8.0="" *="" *="" @param="" string="" $filename="" sanitized="" filename.="" *="" @param="" string="" $filename_raw="" the="" filename="" prior="" to="" sanitization.="" */="" return="" apply_filters(="" 'sanitize_file_name',="" $filename,="" $filename_raw="" );="" }="" process="" multiple="" extensions="" $filename="array_shift($parts);" $extension="array_pop($parts);" $mimes="get_allowed_mime_types();" *="" loop="" over="" any="" intermediate="" extensions.="" postfix="" them="" with="" a="" trailing="" underscore="" *="" if="" they="" are="" a="" 2="" -="" 5="" character="" long="" alpha="" string="" not="" in="" the="" extension="" whitelist.="" */="" foreach="" (="" (array)="" $parts="" as="" $part)="" {="" $filename="" .='.' .="" $part;="" if="" (="" preg_match("/^[a-za-z]{2,5}d?$/",="" $part)="" )="" {="" $allowed="false;" foreach="" (="" $mimes="" as="" $ext_preg=""> $mime_match ) {
				$ext_preg = '!^(' . $ext_preg . ')$!i';
				if ( preg_match( $ext_preg, $part ) ) {
					$allowed = true;
					break;
				}
			}
			if ( !$allowed )
				$filename .= '_';
		}
	}
	$filename .= '.' . $extension;
	/** This filter is documented in wp-includes/formatting.php */
	return apply_filters('sanitize_file_name', $filename, $filename_raw);
}

sanitize_file_name() 源文件

相关