Aller au contenu

regexp php


ckuron

Messages recommandés

Bonjour a tous !

Petite question, j'ai une page php qui fait une requete http et qui parse le retour.

Pour separer le header du content je voudrais utiliser une regexp, mais ca marche pas a tous les coup.


Bien sur les / sont des dans mon code, mais le forum il aime pas trop que je mette des

Voila, normalement ca devrait marcher, mais ca marche pas :), ma regexp me renvoi comme premier match le header + le content et en second match le content seulement.

Lien à poster

Essaie :

preg_match("/(.*?)/r/n/r/n(.*)/s", $buf, $arr);

Pourquoi ?

By default, the quantifiers are "greedy", that is, they match as much as possible (up to the maximum number of permitted times), without causing the rest of the pattern to fail. The classic example of where this gives problems is in trying to match comments in C programs. These appear between the sequences /* and */ and within the sequence, individual * and / characters may appear. An attempt to match C comments by applying the pattern /*.**/ to the string /* first comment */ not comment /* second comment */ fails, because it matches the entire string due to the greediness of the .* item.

However, if a quantifier is followed by a question mark, then it ceases to be greedy, and instead matches the minimum number of times possible, so the pattern /*.*?*/ does the right thing with the C comments. The meaning of the various quantifiers is not otherwise changed, just the preferred number of matches. Do not confuse this use of question mark with its use as a quantifier in its own right. Because it has two uses, it can sometimes appear doubled, as in d??d which matches one digit by preference, but can match two if that is the only way the rest of the pattern matches.

Donc imaginons la chaine "bbaccadd" si je match contre "(.*)a(.*)", la première parenthèse capturante trouvera :

[bbacc]add (la plus grande capture)

[bb]accadd

Elle est gourmande (greedy)

Si par contre je prends "(.*?)a(.*)", elle ne trouvera que

[bb]accadd

Lien à poster
×
×
  • Créer...