'st', 'road' => 'rd', 'avenue' => 'ave', 'lane' => 'ln', 'drive' => 'dr', 'court' => 'ct', 'place' => 'pl', 'square' => 'sq', 'boulevard' => 'blvd', 'highway' => 'hwy', 'terrace' => 'ter', 'apartment' => 'apt', 'flat' => 'apt', 'unit' => 'apt', 'suite' => 'ste', 'floor' => 'fl', 'building' => 'bldg', 'north' => 'n', 'south' => 's', 'east' => 'e', 'west' => 'w', 'number' => 'no', ); /** * Normalized email, or '' when it isn't one. * * @since 1.0.0 * @param string $email Email. * @return string */ public static function email( $email ) { $email = strtolower( trim( (string) $email ) ); if ( 1 !== substr_count( $email, '@' ) ) { return ''; } list( $local, $domain ) = explode( '@', $email ); $local = explode( '+', $local )[0]; if ( in_array( $domain, array( 'gmail.com', 'googlemail.com' ), true ) ) { $local = str_replace( '.', '', $local ); $domain = 'gmail.com'; } return '' === $local || '' === $domain ? '' : $local . '@' . $domain; } /** * Phone key: the last 9 digits, or '' when too short to be a phone number. * * A duplicate-detection heuristic, deliberately loose: two numbers that end in the same 9 * digits are treated as the same phone whatever the country code or trunk prefix, so it can * pair different numbers now and then (the result is only a suggestion staff review). Don't * reuse it where a false match has consequences: the Blocklist power-up * (`Blocklist\Matcher::phone()`) compares the whole number exactly, minus formatting and a * leading 00/0 or country code, because it blocks orders. * * @since 1.0.0 * @param string $phone Phone. * @return string */ public static function phone( $phone ) { $digits = preg_replace( '/\D+/', '', (string) $phone ); if ( strlen( $digits ) < 7 || 1 === count( array_unique( str_split( $digits ) ) ) ) { return ''; // Too short, or placeholder like 0000000. } return substr( $digits, -9 ); } /** * Lowercase ASCII-ish words with punctuation removed. * * @since 1.0.0 * @param string $text Text (accents already removed by the caller where possible). * @return string[] */ public static function words( $text ) { $text = strtolower( (string) $text ); $text = preg_replace( '/[^\p{L}\p{N}]+/u', ' ', $text ); return array_values( array_filter( explode( ' ', trim( (string) $text ) ), static fn( $word ) => '' !== $word ) ); } /** * Normalized street line: words, short forms, "#12" = "12". * * @since 1.0.0 * @param string $line Address line(s). * @return string */ public static function address( $line ) { $words = array_map( static fn( $w ) => self::ABBREVIATIONS[ $w ] ?? $w, self::words( $line ) ); return implode( ' ', $words ); } /** * Postcode without spaces, uppercase. * * @since 1.0.0 * @param string $postcode Postcode. * @return string */ public static function postcode( $postcode ) { return strtoupper( preg_replace( '/[^A-Za-z0-9]+/', '', (string) $postcode ) ); } /** * Token sort ratio (as in fuzzywuzzy), 0–100: words sorted, then a Levenshtein-based ratio. * * @since 1.0.0 * @param string $a First. * @param string $b Second. * @return int */ public static function token_sort_ratio( $a, $b ) { $wa = self::words( $a ); $wb = self::words( $b ); sort( $wa ); sort( $wb ); return self::ratio( implode( ' ', $wa ), implode( ' ', $wb ) ); } /** * Levenshtein ratio, 0–100. * * @since 1.0.0 * @param string $a First. * @param string $b Second. * @return int */ public static function ratio( $a, $b ) { $a = (string) $a; $b = (string) $b; if ( '' === $a && '' === $b ) { return 0; } if ( $a === $b ) { return 100; } $a = substr( $a, 0, 255 ); $b = substr( $b, 0, 255 ); $max = max( strlen( $a ), strlen( $b ) ); return (int) round( ( 1 - levenshtein( $a, $b ) / $max ) * 100 ); } /** * Metaphone of each word, sorted (word order ignored). * * @since 1.0.0 * @param string $name Name. * @return string */ public static function sound( $name ) { $keys = array_filter( array_map( static fn( $w ) => metaphone( $w ), self::words( $name ) ), static fn( $key ) => '' !== $key ); sort( $keys ); return implode( ' ', $keys ); } /** * How alike two full names are, 0–100. Sound-alike names score at least 90; an initial * matches the full first name ("A Silva" / "Ana Silva"). * * @since 1.0.0 * @param string $a First name. * @param string $b Second name. * @return int */ public static function name_score( $a, $b ) { $wa = self::words( $a ); $wb = self::words( $b ); if ( ! $wa || ! $wb ) { return 0; } $score = self::token_sort_ratio( $a, $b ); if ( self::sound( $a ) !== '' && self::sound( $a ) === self::sound( $b ) ) { $score = max( $score, 90 ); } // Initials: same last word, first letters of the other words match. if ( count( $wa ) === count( $wb ) && end( $wa ) === end( $wb ) ) { $initials = true; foreach ( array_slice( $wa, 0, -1 ) as $i => $word ) { $other = $wb[ $i ]; if ( $word[0] !== $other[0] || ( strlen( $word ) > 1 && strlen( $other ) > 1 && $word !== $other ) ) { $initials = false; } } if ( $initials ) { $score = max( $score, 85 ); } } return $score; } /** * How alike two addresses are, 0–100 (0 when postcodes differ and both are set). * * @since 1.0.0 * @param string $line_a Street A. * @param string $postcode_a Postcode A. * @param string $line_b Street B. * @param string $postcode_b Postcode B. * @return int */ public static function address_score( $line_a, $postcode_a, $line_b, $postcode_b ) { $pa = self::postcode( $postcode_a ); $pb = self::postcode( $postcode_b ); if ( '' !== $pa && '' !== $pb && $pa !== $pb ) { return 0; } return self::token_sort_ratio( self::address( $line_a ), self::address( $line_b ) ); } }