Skip to content

Instantly share code, notes, and snippets.

@msaari
Last active April 27, 2018 12:30
Show Gist options
  • Select an option

  • Save msaari/460c635948a363ea378de07d4b5fbc13 to your computer and use it in GitHub Desktop.

Select an option

Save msaari/460c635948a363ea378de07d4b5fbc13 to your computer and use it in GitHub Desktop.
relevanssi_add_accent_variations()
<?php
/**
* Adds accented variations to letters.
*
* In order to have non-accented letters in search terms match the accented terms in
* full text, this function adds accent variations to the search terms.
*
* @param string $word The word to manipulate.
*
* @return string The word with accent variations.
*/
function relevanssi_add_accent_variations( $word ) {
/**
* Filters the accent replacement array.
*
* @param array Array of replacements. 'from' has the source characters, 'to' the replacements.
*/
$replacement_arrays = apply_filters('relevanssi_accents_replacement_arrays', array(
'from' => array( 'a', 'c', 'e', 'i', 'o', 'u', 'n', 'ss' ),
'to' => array( '(a|á|à|â)', '(c|ç)', '(e|é|è|ê|ë)', '(i|í|ì|î|ï)', '(o|ó|ò|ô|õ)', '(u|ú|ù|ü|û)', '(n|ñ)', '(ss|ß)' ),
));
$len = mb_strlen( $word );
$word_array = array();
for ( $i = 0; $i < $len; $i++ ) {
$char = mb_substr( $word, $i, 1 );
$word_array[] = $char;
}
$word = implode( '-?', $word_array );
$word = str_ireplace( $replacement_arrays['from'], $replacement_arrays['to'], $word );
$word = preg_replace( '/s$/', "(s|'s|’s)", $word );
$word = preg_replace( '/^o/', "(o|o'|o’)", $word );
return $word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment