Functions :

* Added function withoutAccents()
  * Improvement of getFData() function :
    + Extracting a portion repeated in the function _getFData_extractAndModify()
    + Added support to negative length in substring selection
    + Added support to make string uppercase or lowercase
    + Added support for removing accents
This commit is contained in:
Benjamin Renard 2010-03-04 16:47:37 +01:00
parent 45d560957b
commit 47c10a169c

View file

@ -39,7 +39,7 @@
*/
function getFData($format,$data,$meth=NULL) {
$unique=false;
$expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?}";
$expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?(-)?(\!|\_)?(~)?}";
if(!is_array($format)) {
$format=array($format);
$unique=true;
@ -54,17 +54,7 @@ function getFData($format,$data,$meth=NULL) {
else {
$val = $data[$ch[1]];
}
if($ch[3]) {
if ($ch[5]) {
$s=$ch[3];
$l=$ch[5];
}
else {
$s=0;
$l=$ch[3];
}
$val=substr((string)$val,$s,$l);
}
$val=_getFData_extractAndModify($val,$ch);
$format[$i]=ereg_replace($ch[0],$val,$format[$i]);
}
}
@ -75,17 +65,7 @@ function getFData($format,$data,$meth=NULL) {
if (is_array($value)) {
$value = $value[0];
}
if($ch[3]) {
if ($ch[5]) {
$s=$ch[3];
$l=$ch[5];
}
else {
$s=0;
$l=$ch[3];
}
$value=substr((string)$value,$s,$l);
}
$value=_getFData_extractAndModify($value,$ch);
$format[$i]=ereg_replace($ch[0],$value,$format[$i]);
}
else {
@ -102,17 +82,7 @@ function getFData($format,$data,$meth=NULL) {
if (is_array($value)) {
$value = $value[0];
}
if($ch[3]) {
if ($ch[5]) {
$s=$ch[3];
$l=$ch[5];
}
else {
$s=0;
$l=$ch[3];
}
$value=substr((string)$value,$s,$l);
}
$value=_getFData_extractAndModify($value,$ch);
$format[$i]=ereg_replace($ch[0],$value,$format[$i]);
}
}
@ -123,17 +93,7 @@ function getFData($format,$data,$meth=NULL) {
if (is_array($value)) {
$value = $value[0];
}
if($ch[3]) {
if ($ch[5]) {
$s=$ch[3];
$l=$ch[5];
}
else {
$s=0;
$l=$ch[3];
}
$value=substr((string)$value,$s,$l);
}
$value=_getFData_extractAndModify($value,$ch);
$format[$i]=ereg_replace($ch[0],$value,$format[$i]);
}
else {
@ -145,11 +105,39 @@ function getFData($format,$data,$meth=NULL) {
}
else {
while (ereg($expr,$format[$i],$ch)) {
$val=_getFData_extractAndModify($data,$ch);
$format[$i]=ereg_replace($ch[0],$val,$format[$i]);
}
}
}
if($unique) {
return $format[0];
}
return $format;
}
function _getFData_extractAndModify($data,$ch) {
if($ch[3]) {
if ($ch[5]) {
if ($ch[6]) {
if ($ch[3]<0) {
$s=strlen($data)-(-1*$ch[3])-$ch[5];
$l=$ch[5];
}
else {
$s=$ch[3]-$ch[5];
$l=$ch[5];
if ($s<0) {
$l=$l-(-1*$s);
$s=0;
}
}
}
else {
$s=$ch[3];
$l=$ch[5];
}
}
else {
$s=0;
$l=$ch[3];
@ -159,14 +147,21 @@ function getFData($format,$data,$meth=NULL) {
else {
$val=$data;
}
$format[$i]=ereg_replace($ch[0],$val,$format[$i]);
# Without Accent
if ($ch[8]) {
$val = withoutAccents($val);
}
# Upper / Lower case
if ($ch[7]=="!") {
$val=strtoupper($val);
}
elseif ($ch[7]=='_') {
$val=strtolower($val);
}
if($unique) {
return $format[0];
}
return $format;
return $val;
}
function getFieldInFormat($format) {
@ -470,4 +465,69 @@ function LSdebugDefined() {
}
}
}
/**
* Supprime les accents d'une chaine
*
* @param[in] $string La chaine originale
*
* @retval string La chaine sans les accents
*/
function withoutAccents($string){
$replaceAccent = Array(
"à" => "a",
"á" => "a",
"â" => "a",
"ã" => "a",
"ä" => "a",
"ç" => "c",
"è" => "e",
"é" => "e",
"ê" => "e",
"ë" => "e",
"ì" => "i",
"í" => "i",
"î" => "i",
"ï" => "i",
"ñ" => "n",
"ò" => "o",
"ó" => "o",
"ô" => "o",
"õ" => "o",
"ö" => "o",
"ù" => "u",
"ú" => "u",
"û" => "u",
"ü" => "u",
"ý" => "y",
"ÿ" => "y",
"À" => "A",
"Á" => "A",
"Â" => "A",
"Ã" => "A",
"Ä" => "A",
"Ç" => "C",
"È" => "E",
"É" => "E",
"Ê" => "E",
"Ë" => "E",
"Ì" => "I",
"Í" => "I",
"Î" => "I",
"Ï" => "I",
"Ñ" => "N",
"Ò" => "O",
"Ó" => "O",
"Ô" => "O",
"Õ" => "O",
"Ö" => "O",
"Ù" => "U",
"Ú" => "U",
"Û" => "U",
"Ü" => "U",
"Ý" => "Y"
);
return strtr($string, $replaceAccent);
}
?>