wc_format_postcode()WC 1.0

Format the postcode according to the country and length of the postcode.

Хуки из функции

Возвращает

Строку.

Использование

wc_format_postcode( $postcode, $country );
$postcode(строка) (обязательный)
Unformatted postcode.
$country(строка) (обязательный)
Base country.

Код wc_format_postcode() WC 7.1.0

function wc_format_postcode( $postcode, $country ) {
	$postcode = wc_normalize_postcode( $postcode );

	switch ( $country ) {
		case 'CA':
		case 'GB':
			$postcode = substr_replace( $postcode, ' ', -3, 0 );
			break;
		case 'IE':
			$postcode = substr_replace( $postcode, ' ', 3, 0 );
			break;
		case 'BR':
		case 'PL':
			$postcode = substr_replace( $postcode, '-', -3, 0 );
			break;
		case 'JP':
			$postcode = substr_replace( $postcode, '-', 3, 0 );
			break;
		case 'PT':
			$postcode = substr_replace( $postcode, '-', 4, 0 );
			break;
		case 'PR':
		case 'US':
			$postcode = rtrim( substr_replace( $postcode, '-', 5, 0 ), '-' );
			break;
		case 'NL':
			$postcode = substr_replace( $postcode, ' ', 4, 0 );
			break;
		case 'LV':
			if ( preg_match( '/(?:LV)?-?(\d+)/i', $postcode, $matches ) ) {
				$postcode = count( $matches ) >= 2 ? "LV-$matches[1]" : $postcode;
			}
			break;
	}

	return apply_filters( 'woocommerce_format_postcode', trim( $postcode ), $country );
}