$table="_____________________________________
Heading 1 | Heading 2
_______________ | ___________________
Label1 18857.10 | Label3 710.00
Label2 2361.50 | Label4 0.00
| Label5 2531.37
| Label6 0.00
| Label7 0.00
| Label8 0.01
________________| ___________________
16495.60 | Label9 3969.06
_______________ | ___________________
";
$num = preg_match_all('/(\w+) (\d+(\.\d+)?)/', $table, $result);
for($i=0; $i<$num; $i++){
echo "{$result[1][$i]} = {$result[2][$i]}<br>";
}
Si votre table correspond exactement à ce que vous avez montré, cela fonctionne.
regex: /(\w+) (\d+(\.\d+)?)/
au /
des barres obliques début et de fin sont délimitant la regex.
(\w+)
moyens, « correspondent à une lettre, un chiffre ou souligner une ou plusieurs fois
un espace suit, vous pouvez ajouter + après l'espace, pour correspondre à plus d'un, ou mettre \ s au lieu de l'espace, à correspond à un caractère blanc, comme onglet par exemple ..
(\d+(\.\d+)?)
... \d+
signifie un ou plusieurs chiffres, des moyens (\.\d+)
point suivi par un ou plusieurs chiffres, point d'interrogation signifie que la parenthèse précédente (\.\d+)
est facultative.
Preg_match_al l stocke ces correspondances dans le troisième paramètre et renvoie le nombre de correspondances. Dans $result[$i][0]
est tout le match, $result[$i][1]
est le premier sous-expression (\w+)
, $result[$i][2]
est deuxième parenthèse (\d+(\.\d+)?)
, $result[$i][3]
est la partie décimale (\.\d+)
, il est à l'intérieur $result[$i][2]
, mais vous n'avez pas besoin $result[$i][3]
, juste pour expliquer :)
les impressions de code:
Heading = 1
Heading = 2
Label1 = 18857.10
Label3 = 710.00
Label2 = 2361.50
Label4 = 0.00
Label5 = 2531.37
Label6 = 0.00
Label7 = 0.00
Label8 = 0.01
Label9 = 3969.06
edit: désolé, ça ne marche pas, il ne correspondait pas cette valeur 16495,60 nue. Laissez-moi réfléchir un peu plus ...
...
$regex='/([a-zA-Z0-9]+)? +(\d+(\.\d+)?)/';
est peu mieux, voici comment cela fonctionne:
[a-zA-Z0-9]+
matchs ammount non nul de lettres ou de chiffres
?
après des parenthèses, l'expression de parenthèse entière est facultative.
+
un ou plusieurs espaces
(\d+(\.\d+)?)
ammount non nul de chiffres suivis par option {point et un autre ammount non nul de chiffres}
Toute cette regex ne comprend pas |
ou une nouvelle ligne, donc toute correspondance devrait se produire dans un seul champ de la table.
La variable résultat devrait être:
array (size=4)
0 =>
array (size=12)
0 => string 'Heading 1' (length=9)
1 => string 'Heading 2' (length=9)
2 => string 'Label1 18857.10' (length=15)
3 => string 'Label3 710.00' (length=13)
4 => string 'Label2 2361.50' (length=14)
5 => string 'Label4 0.00' (length=11)
6 => string 'Label5 2531.37' (length=14)
7 => string 'Label6 0.00' (length=11)
8 => string 'Label7 0.00' (length=11)
9 => string 'Label8 0.01' (length=11)
10 => string ' 16495.60' (length=19)
11 => string 'Label9 3969.06' (length=14)
1 =>
array (size=12)
0 => string 'Heading' (length=7)
1 => string 'Heading' (length=7)
2 => string 'Label1' (length=6)
3 => string 'Label3' (length=6)
4 => string 'Label2' (length=6)
5 => string 'Label4' (length=6)
6 => string 'Label5' (length=6)
7 => string 'Label6' (length=6)
8 => string 'Label7' (length=6)
9 => string 'Label8' (length=6)
10 => string '' (length=0)
11 => string 'Label9' (length=6)
2 =>
array (size=12)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '18857.10' (length=8)
3 => string '710.00' (length=6)
4 => string '2361.50' (length=7)
5 => string '0.00' (length=4)
6 => string '2531.37' (length=7)
7 => string '0.00' (length=4)
8 => string '0.00' (length=4)
9 => string '0.01' (length=4)
10 => string '16495.60' (length=8)
11 => string '3969.06' (length=7)
3 =>
array (size=12)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '.10' (length=3)
3 => string '.00' (length=3)
4 => string '.50' (length=3)
5 => string '.00' (length=3)
6 => string '.37' (length=3)
7 => string '.00' (length=3)
8 => string '.00' (length=3)
9 => string '.01' (length=3)
10 => string '.60' (length=3)
11 => string '.06' (length=3)
Edit2: SAISIR LES Snippets DE NOUVEAU! Il devrait y avoir une barre oblique inverse avant le point, en (\.\d+)
!!! Je l'ai formulé mal et il a disparu. ** Réécrit, ça devrait aller bien maintenant.
Pourriez-vous nous montrer ce que vous avez essayé jusqu'à présent? – Mark