根据列表检查MIME类型。

原型

wp_match_mime_types( string|array $wildcard_mime_types, string|array $real_mime_types )

描述

如果wildcard_mime_types参数是字符串,则它必须是逗号分隔列表。如果real_mime_types是字符串,则它也以逗号分隔以创建列表。

参数

$wildcard_mime_types (string|array) (Required) 哑剧类型,例如audio / mpeg或image(与图像/ 相同)或闪光灯(与 flash *相同)。

$real_mime_types (string|array) (Required) 真正的后mime类型值。

返回值

(array)  array(wildcard => array(real types))。

源文件

路径:wp-includes/post.php

<?php
...
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] ) ) ) {
					$matches[$type][] = $real;
				}
			}
		}
	}
	return $matches;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_match_mime_types/