wp_normalize_path

函数
wp_normalize_path ( $path )
参数
  • (string) $path Path to normalize.
    Required:
返回值
  • (string) Normalized path.
定义位置
相关方法
wp_normalize_site_datawp_localize_scriptnormalize_whitespacewp_get_original_image_pathwp_kses_normalize_entities
引入
3.9.0
弃用
-

wp_normalize_path: 这个函数将文件路径规范化,使其更容易在不同的操作系统中工作。它确保所有的目录分隔符是一致的,并删除任何不必要的”.”或”.”目录引用。

规范化文件系统路径。

在windows系统中,用正斜线代替反斜线,并强制使用大写的驱动器字母。

允许Windows网络共享有两个前导斜杠,但确保所有其他重复的斜杠都减少到一个。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_normalize_path( $path ) {
$wrapper = '';
if ( wp_is_stream( $path ) ) {
list( $wrapper, $path ) = explode( '://', $path, 2 );
$wrapper .= '://';
}
// Standardize all paths to use '/'.
$path = str_replace( '\', '/', $path );
// Replace multiple slashes down to a singular, allowing for network shares having two slashes.
$path = preg_replace( '|(?<=.)/+|', '/', $path );
// Windows paths should uppercase the drive letter.
if ( ':' === substr( $path, 1, 1 ) ) {
$path = ucfirst( $path );
}
return $wrapper . $path;
}
function wp_normalize_path( $path ) { $wrapper = ''; if ( wp_is_stream( $path ) ) { list( $wrapper, $path ) = explode( '://', $path, 2 ); $wrapper .= '://'; } // Standardize all paths to use '/'. $path = str_replace( '\', '/', $path ); // Replace multiple slashes down to a singular, allowing for network shares having two slashes. $path = preg_replace( '|(?<=.)/+|', '/', $path ); // Windows paths should uppercase the drive letter. if ( ':' === substr( $path, 1, 1 ) ) { $path = ucfirst( $path ); } return $wrapper . $path; }
function wp_normalize_path( $path ) {
	$wrapper = '';

	if ( wp_is_stream( $path ) ) {
		list( $wrapper, $path ) = explode( '://', $path, 2 );

		$wrapper .= '://';
	}

	// Standardize all paths to use '/'.
	$path = str_replace( '\', '/', $path );

	// Replace multiple slashes down to a singular, allowing for network shares having two slashes.
	$path = preg_replace( '|(?<=.)/+|', '/', $path );

	// Windows paths should uppercase the drive letter.
	if ( ':' === substr( $path, 1, 1 ) ) {
		$path = ucfirst( $path );
	}

	return $wrapper . $path;
}

常见问题

FAQs
查看更多 >