php 过滤以停止WordPress用短划线替换空格。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 过滤以停止WordPress用短划线替换空格。相关的知识,希望对你有一定的参考价值。

<?php
/**
 * A copy of WordPress' sanitize_file_name function that doesn't replace 
 * spaces with dashes. Use on the sanitize_file_name filter to stop WordPress
 * replacing filenames with dashes.
 *
 * ...Or don't. It's probably a bad idea.
 *
 * @param string $sanitized_filename The previously sanitized filename.
 * @param string $filename The original unsanitized filename.
 * @return string The sanitized filename, but with spaces.
 */
function my_sanitize_file_name( $sanitized_filename, $filename ) {
	$filename_raw = $filename;
	$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
	/**
	 * Filters 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( '/[\r\n\t-]+/', '-', $filename );
	$filename = trim( $filename, '.-_' );

	if ( false === strpos( $filename, '.' ) ) {
		$mime_types = wp_get_mime_types();
		$filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
		if ( $filetype['ext'] === $filename ) {
			$filename = 'unnamed-file.' . $filetype['ext'];
		}
	}

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

	// Return if only one extension
	if ( count( $parts ) <= 2 ) {
		/**
		 * Filters a sanitized filename string.
		 *
		 * @since 2.8.0
		 *
		 * @param string $filename     Sanitized filename.
		 * @param string $filename_raw The filename prior to sanitization.
		 */
		return $filename;
	}

	// 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;

	return $filename;
}
add_filter( 'sanitize_file_name', 'my_sanitize_file_name', 10, 2 );

以上是关于php 过滤以停止WordPress用短划线替换空格。的主要内容,如果未能解决你的问题,请参考以下文章

php WordPress地理产品。取决于ACF的位置元素。替换产品搜索以包括距离查询。

php WordPress删除过滤器(remove_filter转换为remove_class_filter)以删除没有类对象访问的过滤器/操作。与Wor一起使用

访问下划线主题中的非wordpress php文件

php 停止WordPress从猜测URL /停止WordPress自动更正URL

php 停止WordPress从猜测URL /停止WordPress自动更正URL

php正则表达怎么把一个字符串中的所有空格都转化为下划线