1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
$chaine = 'dfsd toto[fgdfg toto dfds]dfg [d toto toto][toto';
function killToto($chaine, $replaceBy){
$sortie = '';
$isKillPossible = false;
$countChaine = strlen($chaine);
$i=0;
while(true){
if($i > $countChaine)
break;
$c = substr($chaine, $i, 1);
if($c == '['){
$isKillPossible = true;
$sortie .= $c;
}elseif($c == ']'){
$isKillPossible = false;
$sortie .= $c;
}elseif(strtolower($c) == 't' && $isKillPossible == true && strtolower(substr($chaine, $i, 4)) == 'toto' && verifNext($chaine, $i)){
$sortie .= $replaceBy;
$i += 4;
continue;
}else{
$sortie .= $c;
}
$i++;
}
return $sortie;
}
function verifNext($chaine, $i){
$countChaine = strlen($chaine);
while(true){
if($i > $countChaine)
return false;
if(substr($chaine, $i, 1) == ']')
return true;
$i++;
}
}
echo killToto($chaine, 'ok');
//Affiche
dfsd toto[fgdfg ok dfds]dfg [d ok ok][toto |
Partager