wc_make_numeric_postcode

Make numeric postcode.

Description

(string) wc_make_numeric_postcode( (string) $postcode ); 

Converts letters to numbers so we can do a simple range check on postcodes. E.g. PE30 becomes 16050300 (P = 16, E = 05, 3 = 03, 0 = 00)

Returns (string)

Parameters (1)

0. $postcode (string)
Regular postcode

Usage

  1. if ( !function_exists( 'wc_make_numeric_postcode' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-formatting-functions.php'; 
  3.  
  4. // Regular postcode 
  5. $postcode = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_make_numeric_postcode($postcode); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-formatting-functions.php  
  1. function wc_make_numeric_postcode( $postcode ) { 
  2. $postcode = str_replace( array( ' ', '-' ), '', $postcode ); 
  3. $postcode_length = strlen( $postcode ); 
  4. $letters_to_numbers = array_merge( array( 0 ), range( 'A', 'Z' ) ); 
  5. $letters_to_numbers = array_flip( $letters_to_numbers ); 
  6. $numeric_postcode = ''; 
  7.  
  8. for ( $i = 0; $i < $postcode_length; $i ++ ) { 
  9. if ( is_numeric( $postcode[ $i ] ) ) { 
  10. $numeric_postcode .= str_pad( $postcode[ $i ], 2, '0', STR_PAD_LEFT ); 
  11. } elseif ( isset( $letters_to_numbers[ $postcode[ $i ] ] ) ) { 
  12. $numeric_postcode .= str_pad( $letters_to_numbers[ $postcode[ $i ] ], 2, '0', STR_PAD_LEFT ); 
  13. } else { 
  14. $numeric_postcode .= '00'; 
  15.  
  16. return $numeric_postcode;