extranet/framework/common/strings.php

419 lines
12 KiB
PHP
Raw Normal View History

<?
define ('BEFORE', 0);
define ('AFTER', 1);
define ('BOTH', 2);
define ('ALIGN_LEFT', 0);
define ('ALIGN_RIGHT', 1);
/**Initialisation d'une cha<EFBFBD>ne de caract<EFBFBD>re
*
* @param string $chaine Cha<EFBFBD>ne de caract<EFBFBD>re initiale
* @param int $taille Taille de la cha<EFBFBD>ne de caract<EFBFBD>re <EFBFBD> initialiser
* @param string $caractere_pour_combler Caract<EFBFBD>re <EFBFBD> utiliser pour combler la cha<EFBFBD>ne de caract<EFBFBD>re (espace par d<EFBFBD>faut)
* @param string $align Aligner la cha<EFBFBD>ne de caract<EFBFBD>re <EFBFBD> droite (right) ou <EFBFBD> gauche (left, par d<EFBFBD>faut)
* @return string
*/
function initstr($chaine, $taille, $caractere_pour_combler=' ', $align=ALIGN_LEFT) {
if ($align==ALIGN_RIGHT) {
$str2='';
for ($i=0;$i<($taille-strlen($chaine));$i++)
$str2.=$caractere_pour_combler;
$str=$str2.$chaine;
} else {
if (strlen($chaine)>=$taille)
return substr($chaine,0,$taille);
$str=$chaine;
for ($i=strlen($chaine);$i<$taille;$i++)
$str = $str . $caractere_pour_combler;
}
return $str;
}
/**
* Ajout d'anti-slashs s'il y a lieu en vu d'une insertion en BDD
*
* @param string $str Chaine de caract<EFBFBD>re
* @return unknown
*/
function checkaddslashes($str){
return addslashes(preg_replace('/\\[^nrt\']/i', '\\', $str));
}
function checkaddslashes2($str){
if(strpos(str_replace("\'",''," $str"),"'")!=false)
return addslashes($str);
else
return $str;
}
function trimAccent ($strWithAccent) {
$strWithAccent = htmlentities(strtolower($strWithAccent ));
$strWithAccent = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml|grave);/", "$1", $strWithAccent );
$strWithAccent = preg_replace("/([^a-z0-9]+)/", " ", html_entity_decode($strWithAccent ));
$strWithAccent = trim($strWithAccent , "-");
return $strWithAccent;
}
//function SRSaufVoyelle ($strIn, $mot1, $mot2) {
function str_replace_except_voy($mot1, $mot2, $strIn, $rule=0) {
$Voyelle=array('a','e','i','o','u','y', '1', '2', '3', '4');
if ($mot1==$mot2) return $strIn;
if (strpos($mot2,$mot1)===false)
{
//foreach ($Voyelle as $k => $voy)
$posMot1=strpos($strIn, $mot1);
while ($posMot1!==false) {
$lettreAV=$strIn[$posMot1-1];
$lettreAP=$strIn[$posMot1+strlen($mot1)];
//echo "Lettre AV=$lettreAV<br/>";
//echo "Lettre AP=$lettreAP<br/>";
if ( ( $rule==0 && !in_array($lettreAV, $Voyelle) ) ||
( $rule==1 && !in_array($lettreAP, $Voyelle) ) ||
( $rule==2 && !in_array($lettreAV, $Voyelle) && !in_array($lettreAP, $Voyelle) ))
$strIn=substr($strIn,0,$posMot1) . $mot2 . substr($strIn,$posMot1+strlen($mot1),strlen($strIn));
//echo "Le Mot devient : $strIn<br/>";
$posMot1=strpos($strIn, $mot1, $posMot1+strlen($mot1));
}
return $strIn;
}
//echo "Erreur : $mot2 contient $mot1 !<br>";
return $strIn;
}
/** Retourne le phonex d'un mot
**/
function phonex($strIn) {
if ($strIn=='') return 0.0;
$tabSonAIA=array('aina', 'eina', 'aima', 'eima');
$tabSonAIE=array('ainr', 'eine', 'aime', 'eime');
$tabSonAII=array('aini', 'eini', 'aimi', 'eimi');
$tabSonAIO=array('aino', 'eino', 'aimo', 'eimo');
$tabSonAIU=array('ainu', 'einu', 'aimu', 'eimu');
$tabCarPhon=array('1', '2', '3', '4', '5', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'n', 'o', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z');
/** On traite tout en minuscule **/
$strIn=strtolower($strIn);
/** On remplace les 'Y' par des 'I' **/
$strIn=str_replace('y', 'i', $strIn);
/** On supprime les accents **/
$strIn=trimAccent($strIn);
/** On retire les 'H' muets sauf ceux pr<70>c<EFBFBD>d<EFBFBD>s par 'C' ou 'S' **/
$strIn = preg_replace ('/(?<![CS])H/', '', $strIn);
/** On remplace les 'PH' par des 'F' **/
$strIn=str_replace('ph', 'f', $strIn);
/** On remplace les 'G' par des 'K' devant AN AM AIN AIM **/
$strIn=str_replace('gan', 'kan', $strIn);
$strIn=str_replace('gain', 'kain', $strIn);
$strIn=str_replace('gam', 'kam4', $strIn);
$strIn=str_replace('gaim', 'kaim', $strIn);
/** On remplace le son AI **/
for ($i=0; $i>4; $i++) {
$strIn=str_replace($tabSonAIA[$i], 'yna', $strIn);
$strIn=str_replace($tabSonAIE[$i], 'yne', $strIn);
$strIn=str_replace($tabSonAII[$i], 'yni', $strIn);
$strIn=str_replace($tabSonAIO[$i], 'yno', $strIn);
$strIn=str_replace($tabSonAIU[$i], 'ynu', $strIn);
}
/** Remplacement des groupes de 3 lettres **/
$strIn=str_replace('eau', 'o', $strIn);
$strIn=str_replace('oua', '2', $strIn);
$strIn=str_replace('ein', '4', $strIn);
$strIn=str_replace('ain', '4', $strIn);
/** Remplacement du son '<27>' **/
$strIn=str_replace('ai', 'y', $strIn);
$strIn=str_replace('ei', 'y', $strIn);
$strIn=str_replace('er', 'yr', $strIn);
$strIn=str_replace('ess', 'yss', $strIn);
$strIn=str_replace('et', 'yt', $strIn);
$strIn=str_replace('ez', 'yz', $strIn);
/** Remplacement des groupes de 2 lettres sauf si voyelle ou son (1 <20> 4) AVANT **/
$strIn=str_replace_except_voy('an', '1', $strIn, BEFORE);
$strIn=str_replace_except_voy('am', '1', $strIn, BEFORE);
$strIn=str_replace_except_voy('en', '1', $strIn, BEFORE);
$strIn=str_replace_except_voy('em', '1', $strIn, BEFORE);
$strIn=str_replace_except_voy('in', '4', $strIn, BEFORE);
/** Remplacement du son 'SCH' **/
$strIn=str_replace('sch', '5', $strIn);
/** Remplacement du 'S' sauf si voyelle ou son (1 <20> 4) avant ou apr<70>s **/
$strIn=str_replace_except_voy('in', '4', $strIn, BOTH);
/** Remplacement de groupe de 2 lettres diverses **/
$strIn=str_replace('oe', 'e', $strIn);
$strIn=str_replace('eu', 'e', $strIn);
$strIn=str_replace('au', 'o', $strIn);
$strIn=str_replace('oi', '2', $strIn);
$strIn=str_replace('oy', '2', $strIn);
$strIn=str_replace('ou', '3', $strIn);
$strIn=str_replace('ch', '5', $strIn);
$strIn=str_replace('sh', '5', $strIn);
$strIn=str_replace('ss', 's', $strIn);
$strIn=str_replace('sc', 's', $strIn);
/** Remplacement du 'C' par 'S' s'il est suivi d'un 'E' ou d'un 'I' **/
$strIn=str_replace('ce', 'se', $strIn);
$strIn=str_replace('ci', 'si', $strIn);
/** Remplacement divers **/
$strIn=str_replace('c', 'k', $strIn);
$strIn=str_replace('q', 'k', $strIn);
$strIn=str_replace('qu', 'k', $strIn);
$strIn=str_replace('ga', 'ka', $strIn);
$strIn=str_replace('go', 'ko', $strIn);
$strIn=str_replace('gu', 'ku', $strIn);
$strIn=str_replace('gy', 'ky', $strIn);
$strIn=str_replace('g2', 'k2', $strIn);
$strIn=str_replace('g1', 'k1', $strIn);
$strIn=str_replace('g3', 'k3', $strIn);
$strIn=str_replace('a', 'o', $strIn);
$strIn=str_replace('d', 't', $strIn);
$strIn=str_replace('p', 't', $strIn);
$strIn=str_replace('j', 'g', $strIn);
$strIn=str_replace('b', 'f', $strIn);
$strIn=str_replace('v', 'f', $strIn);
$strIn=str_replace('m', 'n', $strIn);
/** Supression des lettres dupliqu<71>es **/
$let=$strIn[0];
$strIn2=$let;
for ($i=1; $i<strlen($strIn); $i++)
{ if ($strIn==$let)
continue;
else {
$let=$strIn[$i];
$strIn2.=$strIn[$i];
}
}
$strIn=$strIn2;
/** Supression des terminaisons **/
$strIn2=substr($strIn,-1);
if ($strIn2=='t' || $strIn2=='k' || $strIn2=='s' || $strIn2=='z')
$strIn=substr($strIn,0,-1);
/** Supression des caract<63>res non autoris<69>s **/
$j=10;
$sout=array();
for ($i=0; $i<strlen($strIn); $i++)
{
if ($j<1) break;
for ($k=0; $k<22; $k++)
{
if ($strIn[$i]==$tabCarPhon[$k])
{
$sout[$j]=$k;
$j--;
}
}
}
print_r($tabCarPhon);
/** Couversion en flottant **/
$result=0.0;
for ($j=10; $j>0; $j--)
$result+=$sout[$j]*pow($j-1,10);
return $result;
}
/**
* CLASS soundex2
* soundex2 French version
* based on the algorithm described here : http://sqlpro.developpez.com/cours/soundex/ by Fr<EFBFBD>d<EFBFBD>ric BROUARD
*
* author Johan Barbier <barbier_johan@hotmail.com>
*/
class csoundex2 {
/**
* public sString
* main string we work on
*/
var $sString = '';
/**
* vowels replacement array
*/
var $aReplaceVoy1 = array (
'E' => 'A',
'I' => 'A',
'O' => 'A',
'U' => 'A'
);
/**
* consonnants replacement array
*/
var $aReplaceGrp1 = array (
'GUI' => 'KI',
'GUE' => 'KE',
'GA' => 'KA',
'GO' => 'KO',
'GU' => 'K',
'CA' => 'KA',
'CO' => 'KO',
'CU' => 'KU',
'Q' => 'K',
'CC' => 'K',
'CK' => 'K'
);
/**
* other replacement array
*/
var $aReplaceGrp2 = array (
'ASA' => 'AZA',
'KN' => 'NN',
'PF' => 'FF',
'PH' => 'FF',
'SCH' => 'SSS'
);
/**
* endings replacement array
*/
var $aEnd = array (
'A',
'T',
'D',
'S'
);
/**
* public function build
* core function of the class, go through the whole process
* @Param string sString : the string we want to check
*/
function build ($sString) {
/**
* let's check it's a real string...
*/
if (is_string ($sString) && !empty ($sString)) {
$this -> sString = $sString;
} else {
trigger_error ('Parameter string must not be empty', E_USER_ERROR);
}
/**
* remove starting and ending spaces
*/
$this -> sString = trim ($this -> sString);
/**
* remove special french characters
*/
$this -> trimAccent ();
/**
* string to upper case
*/
$this -> sString = strtoupper ($this -> sString );
/**
* let's remove every space in the string
*/
$this -> sString = str_replace (' ', '', $this -> sString);
/**
* let's remove every '-' in the string
*/
$this -> sString = str_replace ('-', '', $this -> sString);
/**
* let's process through the first replacement array
*/
$this -> arrReplace ($this -> aReplaceGrp1);
/**
* let's process through th vowels replacement
*/
$sChar = substr ($this -> sString, 0, 1);
$this -> sString = substr ($this -> sString, 1, strlen ($this -> sString) - 1);
$this -> arrReplace ($this -> aReplaceVoy1);
$this -> sString = $sChar.$this -> sString;
/**
* let's process through the second replacement array
*/
$this -> arrReplace ($this -> aReplaceGrp2, true);
/**
* let's remove every 'H' but those prededed by a 'C' or an 'S'
*/
$this -> sString = preg_replace ('/(?<![CS])H/', '', $this -> sString);
/**
* let's remove every 'Y' but those preceded by an 'A'
*/
$this -> sString = preg_replace ('/(?<!A)Y/', '', $this -> sString);
/**
* remove endings in aEnd
*/
$length = strlen ($this -> sString) - 1;
if (in_array ($this -> sString{$length}, $this -> aEnd)) {
$this -> sString = substr ($this -> sString, 0, $length);
}
/**
* let's remove every 'A', but the one at the beginning of the string, if any.
*/
$sChar = '';
if ($this -> sString{0} === 'A') {
$sChar = 'A';
}
$this -> sString = str_replace ('A', '', $this -> sString);
$this -> sString = $sChar.$this -> sString;
/**
* let's have only 1 occurence of each letter
*/
$this -> sString = preg_replace( '/(.)\1/', '$1', $this -> sString );
/**
* let's have the final code : a 4 letters string
*/
$this -> getFinal ();
}
/**
* private function getFinal
* gets the first 4 letters, pads the string with white space if the string length < 4
*/
function getFinal () {
if (strlen ($this -> sString) < 4) {
$this -> sString = str_pad ($this -> sString, 4, ' ', STR_PAD_RIGHT);
} else {
$this -> sString = substr ($this -> sString, 0, 4);
}
}
/**
* private function trimAccent
* remove every special French letters
*/
function trimAccent () {
$this -> sString = htmlentities(strtolower($this -> sString ));
$this -> sString = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml|grave);/", "$1", $this -> sString );
$this -> sString = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($this -> sString ));
$this -> sString = trim($this -> sString , "-");
}
/**
* private function arrReplace
* replacement method, given an array
* @Param array tab : the replacement array to be used
* @Param bool pref : if false, just replace keys by values; if true, do the same but only with prefix
*/
function arrReplace ($tab, $pref = false) {
$fromRep = array_keys ($tab);
$toRep = array_values ($tab);
if (false === $pref) {
$this -> sString = str_replace ($fromRep, $toRep, $this -> sString);
} else {
foreach ($fromRep as $clef => $val) {
$length = strlen ($val);
if (substr ($this -> sString, 0, $length) === $val) {
$this -> sString = substr_replace ($this -> sString, $toRep[$clef], 0, $length);
}
}
}
}
}
function soundex2($str) {
$soundex2 = new csoundex2();
$soundex2 -> build ($str);
return $soundex2 -> sString;
}
?>