From ba78f5168198c4510941509330892a62dcb395e6 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 27 Oct 2008 14:06:49 +0000 Subject: [PATCH] =?UTF-8?q?-=20getFData=20(Php&JS)=20:=20Ajout=20d'une=20p?= =?UTF-8?q?ossibilit=C3=A9=20de=20d=C3=A9coupage=20de=20la=20valeur=20de?= =?UTF-8?q?=20remplacement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trunk/includes/functions.php | 64 ++++++++++++++++++++++++++++++---- trunk/includes/js/functions.js | 41 +++++++++++++++++----- 2 files changed, 90 insertions(+), 15 deletions(-) diff --git a/trunk/includes/functions.php b/trunk/includes/functions.php index 17c5fdc8..b505be2c 100644 --- a/trunk/includes/functions.php +++ b/trunk/includes/functions.php @@ -39,6 +39,7 @@ */ function getFData($format,$data,$meth=NULL) { $unique=false; + $expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?}"; if(!is_array($format)) { $format=array($format); $unique=true; @@ -46,17 +47,42 @@ function getFData($format,$data,$meth=NULL) { for($i=0;$i $meth(); 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); + } $format[$i]=ereg_replace($ch[0],$value,$format[$i]); } else { @@ -68,16 +94,42 @@ function getFData($format,$data,$meth=NULL) { } else { if ($meth==NULL) { - while (ereg("%{([A-Za-z0-9]+)}",$format[$i],$ch)) - $format[$i]=ereg_replace($ch[0],$data,$format[$i]); + while (ereg($expr,$format[$i],$ch)) { + if($ch[3]) { + if ($ch[5]) { + $s=$ch[3]; + $l=$ch[5]; + } + else { + $s=0; + $l=$ch[3]; + } + $val=substr((string)$data,$s,$l); + } + else { + $val=$data; + } + $format[$i]=ereg_replace($ch[0],$val,$format[$i]); + } } else { - while (ereg("%{([A-Za-z0-9]+)}",$format[$i],$ch)) { + while (ereg($expr,$format[$i],$ch)) { if (method_exists($data,$meth)) { $value = $data -> $meth($ch[1]); 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); + } $format[$i]=ereg_replace($ch[0],$value,$format[$i]); } else { diff --git a/trunk/includes/js/functions.js b/trunk/includes/js/functions.js index 1a986120..c46dfa3c 100644 --- a/trunk/includes/js/functions.js +++ b/trunk/includes/js/functions.js @@ -34,17 +34,29 @@ function LSdebug() { * @retval string La chaine formatée */ function getFData(format,data,meth) { - var getMotif = new RegExp('%\{([A-Za-z0-9]+)\}'); + var getMotif = new RegExp('%\{(([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?)\}'); var find=1; + var val=""; if(($type(data)=='object') || ($type(data)=='array')) { if ($type(data[meth])!='function') { while (find) { var ch = getMotif.exec(format); if ($type(ch)) { - format=format.replace ( - new RegExp('%\{'+ch[1]+'\}'), - data[ch[1]] - ); + if($type(ch[4])) { + if ($type(ch[6])) { + var s=ch[4]; + var l=ch[6]; + } + else { + var s=0; + var l=ch[4]; + } + var val=data[ch[2]].substr(s,l); + } + else { + val=data[ch[2]]; + } + format=format.replace(new RegExp('%\{'+ch[1]+'\}'),val); } else { find=0; @@ -56,14 +68,25 @@ function getFData(format,data,meth) { var ch = getMotif.exec(format); if ($type(ch)) { try { - format=format.replace ( - new RegExp('%\{'+ch[1]+'\}'), - data[meth](ch[1]) - ); + val=data[meth](ch[2]); } catch(e) { return; } + + if($type(ch[4])) { + if ($type(ch[6])) { + var s=ch[4]; + var l=ch[6]; + } + else { + var s=0; + var l=ch[4]; + } + val=val.substr(s,l); + } + + format=format.replace(new RegExp('%\{'+ch[1]+'\}'),val); } else { find=0;