[RESOLU] Aide pour traitement chaine de caractères

Petit nouveau ! | 8 Messages

02 déc. 2014, 13:36

Bonjour je suis débutant en php,
Voici une ligne de mon fichier texte.
Ceci est une ligne : GRE_JEAN_JAURES;NW/UTRAN BTSEquipment/GRE_JEAN_JAURES Board/723467;10-04-2014 11:14:28;null;processing error;warning;active;softwareProgramError;TRDU/11 SOFTWARE DUMP;TRDU/11 SOFTWARE DUMP Specific Problem:TRDU/11 SOFTWARE DUMP, Cabinet:11, Shelf:10, Slot-position:11, AdditionalInformationFromNE:additionalInformation = { manufacturerInfoAscii = GP_TRDU Pmdz :The system was power-cycled. - Cause:1 - RRH F/W:_BI_RE_TE_TR_BAND01_FV_1.2.18 - HwRel:AB04 - PEC:3JR21012AB - SN: GPCSTW1000501913 - MOD:TRDUBAND014C60W - SWver:UNB0814A110 } ;BTS_0002_00064;
Le but est de prendre le mot PEC: tout ce qui suit jusqu'au tiret en exemple ici PEC:3JR21012AB . Et j'y arrive à l'aide de ces deux fonctions ci dessous. Le problème en est que parfois sur certaines lignes du fichier le mot a recherché est comme ça en exemple :PE C:3JR21012AB avec un blanc au niveau du PEC. Et je voudrais à partir de ces deux fonctions, s'il voit "PEC" ou "PE C"( avec un blanc entre PE et le C), qu'il fasse le même traitement.
Merci de vos retours.
Bien cordialement.
function after ($this, $inthat)
{
    if (!is_bool(strpos($inthat, $this)))
        return substr($inthat, strpos($inthat,$this)+strlen($this));
};

function before ($this, $inthat)
{
    return substr($inthat, 0, strpos($inthat, $this));
};


function between ($this, $that, $inthat)
{
    return before ($that, after($this, $inthat));
};

$sLine = "GRE_JEAN_JAURES;NW/UTRAN BTSEquipment/GRE_JEAN_JAURES Board/723467;10-04-2014 11:14:28;null;processing error;warning;active;softwareProgramError;TRDU/11 SOFTWARE DUMP;TRDU/11 SOFTWARE DUMP Specific Problem:TRDU/11 SOFTWARE DUMP, Cabinet:11, Shelf:10, Slot-position:11, AdditionalInformationFromNE:additionalInformation = { manufacturerInfoAscii = GP_TRDU Pmdz :The system was power-cycled. - Cause:1 - RRH F/W:_BI_RE_TE_TR_BAND01_FV_1.2.18 - HwRel:AB04 - PEC:3JR21012AB - SN: GPCSTW1000501913 - MOD:TRDUBAND014C60W - SWver:UNB0814A110 } ;BTS_0002_00064;";

echo 'PEC'.between('PEC','-',$sLine);

$sLineSecond = "CINQ_AVENUES;NW/UTRAN BTSEquipment/CINQ_AVENUES Board/81;10-04-2014 15:47:45;null;processing error;warning;active;softwareProgramError;CCM/81 SOFTWARE DUMP;CCM/81 SOFTWARE DUMP Specific Problem:CCM/81 SOFTWARE DUMP, Cabinet:0, Shelf:0, Slot-position:81, AdditionalInformationFromNE:additionalInformation = { manufacturerInfoAscii = Uptime : 52 days 04:05:43.Sequence num: 32.Thread name : tCpManager.Thread ID : 0x36ddb4b0.Error number: 0.Signal : 11.State and Misc Registers:. TRAP : 00000300. MSR : 0202d200. DAR : 00000096. DSISR: 00800000. IAR : 0d39299c. SP : 36dda7e0. LR : 0d391810. CR : 24004442. CTR : 0cdb1d60. XER : 00000000.Traceback:. 0d39299c. 0d3e1a80. 0d407f64. 0 f79ef74. 0d47c590. 0d47d00c. 0f822178. 0fd22ccc. 30028d8c. 0ff25a90. 00000000. 00000000. 00000000. 00000000. 00000000 . 00000000. 00000000. 00000000. 00000000. 00000000.HwRel:CA01 - PEC:3JR20073BC - SN:YP14160B64C - MOD:MOD:eCCM-U_GE - SWver :UNB0814A110 } ;BTS_0002_00001;";

echo 'PEC'.between('PEC','-',$sLineSecond);

Eléphanteau du PHP | 12 Messages

02 déc. 2014, 14:09

Salut j'imagine que si c'est écrit "PE C" dans ta ligne ta fonction between avec 'PEC' va te renvoyer une chaîne de caractères... vide.
Tu peux faire un petit test si ça marche pas, genre
$between=between('PEC','-',$sLineSecond);
if ($between=='') $between=between('PE C','-',$sLineSecond);
echo 'PEC'.$between;

C'est peut-être pas très fin mais ça doit marcher :)

Petit nouveau ! | 8 Messages

05 déc. 2014, 02:26

Salut j'imagine que si c'est écrit "PE C" dans ta ligne ta fonction between avec 'PEC' va te renvoyer une chaîne de caractères... vide.
Tu peux faire un petit test si ça marche pas, genre
$between=between('PEC','-',$sLineSecond);
if ($between=='') $between=between('PE C','-',$sLineSecond);
echo 'PEC'.$between;

C'est peut-être pas très fin mais ça doit marcher :)
Merci Basile, d'avoir prêté attention à mon problème. J'avais pensé à ta solution mais bizarrement ça ne marche pas, il ne m'affiche rien. Cependant il rentre bien dans le test fu if quand la chaine de caractères est vide.
Bien cdt.

Nestecha
Invité n'ayant pas de compte PHPfrance

05 déc. 2014, 02:55

? Ici ça fonctionne, essaie en copiant mon code :
<?php
function after ($this, $inthat)
{
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
};

function before ($this, $inthat)
{
return substr($inthat, 0, strpos($inthat, $this));
};


function between ($this, $that, $inthat)
{
return before ($that, after($this, $inthat));
};

function getPEC($sString) {
    if (between('PEC','-',$sString) == '') {
        return 'PEC'.between('PE C','-',$sString);
    } else {
        return 'PEC'.between('PEC','-',$sString);
    }
}

$sLine = "GRE_JEAN_JAURES;NW/UTRAN BTSEquipment/GRE_JEAN_JAURES Board/723467;10-04-2014 11:14:28;null;processing error;warning;active;softwareProgramError;TRDU/11 SOFTWARE DUMP;TRDU/11 SOFTWARE DUMP Specific Problem:TRDU/11 SOFTWARE DUMP, Cabinet:11, Shelf:10, Slot-position:11, AdditionalInformationFromNE:additionalInformation = { manufacturerInfoAscii = GP_TRDU Pmdz :The system was power-cycled. - Cause:1 - RRH F/W:_BI_RE_TE_TR_BAND01_FV_1.2.18 - HwRel:AB04 - PEC:3JR21012AB - SN: GPCSTW1000501913 - MOD:TRDUBAND014C60W - SWver:UNB0814A110 } ;BTS_0002_00064;";

echo getPEC($sLine);

$sLineSecond = "CINQ_AVENUES;NW/UTRAN BTSEquipment/CINQ_AVENUES Board/81;10-04-2014 15:47:45;null;processing error;warning;active;softwareProgramError;CCM/81 SOFTWARE DUMP;CCM/81 SOFTWARE DUMP Specific Problem:CCM/81 SOFTWARE DUMP, Cabinet:0, Shelf:0, Slot-position:81, AdditionalInformationFromNE:additionalInformation = { manufacturerInfoAscii = Uptime : 52 days 04:05:43.Sequence num: 32.Thread name : tCpManager.Thread ID : 0x36ddb4b0.Error number: 0.Signal : 11.State and Misc Registers:. TRAP : 00000300. MSR : 0202d200. DAR : 00000096. DSISR: 00800000. IAR : 0d39299c. SP : 36dda7e0. LR : 0d391810. CR : 24004442. CTR : 0cdb1d60. XER : 00000000.Traceback:. 0d39299c. 0d3e1a80. 0d407f64. 0 f79ef74. 0d47c590. 0d47d00c. 0f822178. 0fd22ccc. 30028d8c. 0ff25a90. 00000000. 00000000. 00000000. 00000000. 00000000 . 00000000. 00000000. 00000000. 00000000. 00000000.HwRel:CA01 - PE C:3JR20073BC - SN:YP14160B64C - MOD:MOD:eCCM-U_GE - SWver :UNB0814A110 } ;BTS_0002_00001;";

echo getPEC($sLineSecond);
 
Note que j'ai ajouté un espace à PEC:3JR20073BC dans la deuxième chaîne pour obtenir PE C:3JR20073BC.

ViPHP
ViPHP | 928 Messages

05 déc. 2014, 12:04

Salut,

Les regexp sont faîtes pour répondre à ce genre de problématiques, voici une version beaucoup plus simple et souple pour résoudre ce problème :
function getPEC($line)
{
	if (preg_match('#-\s*(P\s*E\s*C:[A-Z0-9]+)(\s*-|\s*$)#', $line, $m))
	{
		return $m[1];
	}
	return false;
}

$sLine = "CINQ_AVENUES;NW/UTRAN BTSEquipment/CINQ_AVENUES Board/81;10-04-2014 15:47:45;null;processing error;warning;active;softwareProgramError;CCM/81 SOFTWARE DUMP;CCM/81 SOFTWARE DUMP Specific Problem:CCM/81 SOFTWARE DUMP, Cabinet:0, Shelf:0, Slot-position:81, AdditionalInformationFromNE:additionalInformation = { manufacturerInfoAscii = Uptime : 52 days 04:05:43.Sequence num: 32.Thread name : tCpManager.Thread ID : 0x36ddb4b0.Error number: 0.Signal : 11.State and Misc Registers:. TRAP : 00000300. MSR : 0202d200. DAR : 00000096. DSISR: 00800000. IAR : 0d39299c. SP : 36dda7e0. LR : 0d391810. CR : 24004442. CTR : 0cdb1d60. XER : 00000000.Traceback:. 0d39299c. 0d3e1a80. 0d407f64. 0 f79ef74. 0d47c590. 0d47d00c. 0f822178. 0fd22ccc. 30028d8c. 0ff25a90. 00000000. 00000000. 00000000. 00000000. 00000000 . 00000000. 00000000. 00000000. 00000000. 00000000.HwRel:CA01 - PE C:3JR20073BC - SN:YP14160B64C - MOD:MOD:eCCM-U_GE - SWver :UNB0814A110 } ;BTS_0002_00001;";
echo getPEC($sLine);
A toi de déterminer quel est le format exact de ta chaîne. Par exemple dans la regexp que j'ai mis, tu peux avoir autant d'espaces que tu veux entre les lettres "P", "E" et "C" de "PEC" (\s* signifie "X espaces, avec X >= 0)