[utilescript] substr_replace : remplace un segment dans une chaîne

Description

Fonctionne exactement comme PHP.

mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

substr_replace() remplace un segment de la chaîne string par la chaîne replacement. Le segment est délimité par start et éventuellement par length.

- string

La chaîne d'entrée.

Un tableau de chaînes de caractères peut être fourni, et dans ce cas, les remplacements surviendront sur chacune des chaînes. Dans cette situation, les paramètres replacement, start length doivent être fournis soit comme valeurs scalaires à appliquer sur chaque chaîne, ou comme tableaux où l'élément du tableau correspondant sera utilisé pour chaque chaîne d'entrée.

- replacement

La chaîne de remplacement.

- start

Si start est positif, le remplacement se fera à partir du caractère numéro start dans string.

Si start est négatif, le remplacement se fera à partir du start-ième caractère en partant de la fin de la chaîne string.

- length

Si length est fourni et positif, il représentera la longueur du segment de code remplacé dans la chaîne string. S'il est négatif, il représentera la longueur du segment remplacé, mais compté dans l'ordre inverse de la chaîne string. S'il est omis, il prendra la valeur par défaut de la taille de la chaîne, et remplacera tout jusqu'à la fin de la chaîne string. Bien sûr, si length vaut 0, alors, cette fonction aura comme effet d'insérer replacement dans string à la position start donnée.

Source / Exemple :


/*Crée par Tobie519 avec Adobe Dreamweaver
Site : CodeS-SourceS*/
function substr_replace(str, repl, start, length){
	// Parrametres
	if(typeof(length)=="undefined"){
		length = str;
		if(typeof(length)=="object"){
			for(i=0;i!=length;i++){
				length[i]=length[i].length;
			}
		}else{
			length = length.length;
		}
	}
	if(typeof(start)=="object"){
		for(i=0;i!=start.length;i++){
			start[i]=parseInt(start[i]);
		}
	}else{
		start = parseInt(start);
	}
	if(typeof(length)=="object"){
		for(i=0;i!=length.length;i++){
			length[i]=parseInt(length[i]);
		}
	}else{
		length = parseInt(length);
	}
	x = Array();
	// Operation Principale
	function sub_rep_op(opstr, oprepl, opstart, oplength){
		if(opstart<0){opstart = opstr.length+opstart;}
		if(oplength<0){
			cut1 = opstr.substring(0, opstart+oplength);
			cut2 = opstr.substring(opstart+oplength, opstart);
			cut3 = opstr.substring(opstart, opstr.length);
		}else{
			cut1 = opstr.substring(0, opstart);
			cut2 = opstr.substring(opstart, opstart+oplength);
			cut3 = opstr.substring(opstart+oplength, opstr.length);
		}
		cut2 = oprepl;
		return cut1+cut2+cut3;
	}
	// Création du tableau des tâches
	tache = Array();
	if(typeof(str)=="string"){
			tache[0] = Array(str);
	}else if(typeof(str)=="object"&&typeof(str[0])=="string"){
		for(i=0;i!=str.length;i++){
			tache[i] = Array(str[i]);
		}
	}
	for(i=0;i!=tache.length; i++){
		// Replacement
		if(typeof(repl)=="string"){
			// String Replacement
			tache[i][1]=repl;
		}else if(tache.length==1){
			// Array non-Relative Replacement
			tache[i][1]=repl;
		}else{
			// Array Relative Replacement
			tache[i][1]=repl[i];
		}
		// Start
		if(typeof(start)=="number"){
			// String Start
			tache[i][2]=start;
		}else if(tache.length==1){
			// Array non-Relative Start
			tache[i][2]=start;
		}else{
			// Array Relative Start
			tache[i][2]=start[i];
		}
		// Length
		if(typeof(length)=="number"){
			// String Length
			tache[i][3]=length;
		}else if(tache.length==1){
			// Array non-Relative Length
			tache[i][3]=length;
		}else{
			// Array Relative Length
			tache[i][3]=length[i];
		}
	}
	for(i=0;i!=tache.length;i++){
		opStr = tache[i][0];
		opRep = tache[i][1];
		opSta = tache[i][2];
		opLen = tache[i][3];
		op = Array();
		count = 1;
		if(typeof(opRep)=="object"&&opRep.length>count){
			count = opRep.length;
		}
		if(typeof(opSta)=="object"&&opSta.length>count){
			count = opSta.length;
		}
		if(typeof(opLen)=="object"&&opLen.length>count){
			count = opLen.length;
		}
		for(j=0;j!=count;j++){
			op[j] = Array();
			// Replacement
			if(typeof(opRep)=="string"){
				// String Replacement
				op[j][0]=opRep;
			}else{
				// Array Relative Replacement
				op[j][0]=opRep[j];
			}
			// Start
			if(typeof(opSta)=="number"){
				// String Start
				op[j][1]=opSta;
			}else{
				// Array Relative Start
				op[j][1]=opSta[j];
			}
			// Length
			if(typeof(opLen)=="number"){
				// String Length
				op[j][2]=opLen;
			}else{
				// Array Relative Length
				op[j][2]=opLen[j];
			}
		}
		syncString=opStr;
		for(j=0;j!=op.length;j++){
			syncString = sub_rep_op(syncString, op[j][0],op[j][1],op[j][2]);
		}
		x[i] = syncString;
	}
	if(x.length==1){
		return x[0];
	}else{
		return x;
	}
}

Conclusion :


Remplace un segment dans une chaîne

Codes Sources

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.