函数文档

wp_match_mime_types()

💡 云策文档标注

概述

wp_match_mime_types() 函数用于将 MIME 类型与一个列表进行匹配检查,支持通配符和数组或逗号分隔的字符串输入。它返回一个关联数组,表示通配符 MIME 类型与真实 MIME 类型之间的匹配关系。

关键要点

  • 函数接受两个参数:$wildcard_mime_types 和 $real_mime_types,均可为字符串或数组,字符串需用逗号分隔。
  • 内部处理包括将字符串参数转换为数组,并基于通配符(如 *)生成正则表达式模式进行匹配。
  • 返回数组格式为 array(wildcard=>array(real types)),便于在 WordPress 开发中处理媒体上传和 MIME 类型验证。

代码示例

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 ( ! str_contains( $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;
}

📄 原文内容

Checks a MIME-Type against a list.

Description

If the $wildcard_mime_types parameter is a string, it must be comma separated list. If the $real_mime_types is a string, it is also comma separated to create the list.

Parameters

$wildcard_mime_typesstring|string[]required
Mime types, e.g. audio/mpeg, image (same as image/*), or flash (same as *flash*).
$real_mime_typesstring|string[]required
Real post mime type values.

Return

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

Source

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 ( ! str_contains( $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;
}

Changelog

Version Description
2.5.0 Introduced.