wp_match_mime_types

函数
wp_match_mime_types ( $wildcard_mime_types, $real_mime_types )
参数
  • (string|string[]) $wildcard_mime_types Mime types, e.g. `audio/mpeg`, `image` (same as `image/*`), or `flash` (same as `*flash*`).
    Required:
  • (string|string[]) $real_mime_types Real post mime type values.
    Required:
返回值
  • (array) array(wildcard=>array(real types)).
定义位置
相关方法
wp_get_mime_typeswp_mime_type_iconwp_post_mime_type_where_wp_batch_update_comment_type_wp_batch_split_terms
引入
2.5.0
弃用
-

wp_match_mime_types: 这是一个用来匹配文件扩展名和MIME类型的函数。它返回与给定文件扩展名相关的MIME类型。

根据一个列表检查一个MIME-Type。

如果`$wildcard_mime_types`参数是一个字符串,它必须是逗号分隔的列表。如果`$real_mime_types`是一个字符串,也要用逗号分隔来创建列表。

function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
	$matches = array();
	if ( is_string( $wildcard_mime_types ) ) {
		$wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) );
	}
	if ( is_string( $real_mime_types ) ) {
		$real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) );
	}

	$patternses = array();
	$wild       = '[-._a-z0-9]*';

	foreach ( (array) $wildcard_mime_types as $type ) {
		$mimes = array_map( 'trim', explode( ',', $type ) );
		foreach ( $mimes as $mime ) {
			$regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) );

			$patternses[][ $type ] = "^$regex$";

			if ( false === strpos( $mime, '/' ) ) {
				$patternses[][ $type ] = "^$regex/";
				$patternses[][ $type ] = $regex;
			}
		}
	}
	asort( $patternses );

	foreach ( $patternses as $patterns ) {
		foreach ( $patterns as $type => $pattern ) {
			foreach ( (array) $real_mime_types as $real ) {
				if ( preg_match( "#$pattern#", $real )
					&& ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) )
				) {
					$matches[ $type ][] = $real;
				}
			}
		}
	}

	return $matches;
}

常见问题

FAQs
查看更多 >