Vérifie la validité d'un code HTML

Vérifie la syntaxe d'un fichier HTML. Retourne un tableau associatif rendant compte des erreurs contenues dans le fichier (ouverture/fermeture, placement et emboîtement des balises). Auteur : Cedric Fronteau


Information sur les mises à jour

Dernière mise à jour :

13 Août 2019
fonctionnement du code vérifié
maintenance du code

2 511  vues
Compatibilité du code
PHP 5, PHP 7 et PHP 8
  code classé dans   HTML
  code source classé dans   HTML
 
01
02
03
04
05
06
07
08
09
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
                    
<?php
/*------------------------------*/
/*
Titre : Vérifie la validité d'un code HTML

Date édition : 14 Fev 2019
Date mise a jour : 13 Aout 2019

Rapport de la maj:
- fonctionnement du code vérifié
- maintenance du code
*/
/*------------------------------*/

function html_check($url){
/* Parametre : $url : URL ou chemin d'acces du fichier a tester
* La variable retournée est 1 tableau associatif contenant des
tableaux:
* "ouvertures multiples" => tableau des balises concernées,

* "non ouverte" => tableau a 2 dimensions (balise -> nombre)
* "non fermee" => tableau a 2 dimensions (balise -> nombre)
* "mal placee" => tableau a 2 dimensions (balise -> nombre)
* "mal emboitee" => tableau a 2 dimensions
* (balise fermante -> balise ouvrante en conflit)
* "inconnue" => tableau des balises inconnues
*/

$notonlyonce = array("TABLE", "FRAMESET");
$openclosetags = array(
"A" => 0,
"ADDRESS" => 0,
"APPLET" => 0,
"B" => 0,
"BIG" => 0,
"BLINK" => 0,
"BLOCKQUOTE" => 0,
"BODY" => 0,
"CAPTION" => 0,
"CENTER" => 0,
"CITE" => 0,
"CODE" => 0,
"COMMENT" => 0,
"DD" => 0,
"DFN" => 0,
"DIR" => 0,
"DIV" => 0,
"DL" => 0,
"DT" => 0,
"DT" => 0,
"EM" => 0,
"EMBED" => 0,
"FONT" => 0,
"FORM" => 0,
"FRAME" => 0,
"FRAMESET" => 0,
"H1" => 0, "H2" => 0, "H3" => 0, "H4" => 0, "H5" => 0, "H6" => 0,
"HEAD" => 0,
"HTML" => 0,
"I" => 0,
"IFRAME" => 0,
"ILAYER" => 0,
"KBD" => 0,
"LAYER" => 0,
"LISTING" => 0,
"MAP" => 0,
"MARQUEE" => 0,
"MENU" => 0,
"MULTICOL" => 0,
"NOBR" => 0,
"NOEMBED" => 0,
"NOFRAMES" => 0,
"NOLAYER" => 0,
"NOSCRIPT" => 0,
"OBJECT" => 0,
"OL" => 0,
"OPTION" => 0,
"PARAM" => 0,
"PRE" => 0,
"S" => 0,
"SAMP" => 0,
"SCRIPT" => 0,
"SELECT" => 0,
"SERVER" => 0,
"SMALL" => 0,
"SPAN" => 0,
"STRIKE" => 0,
"STRONG" => 0,
"STYLE" => 0,
"SUB" => 0,
"SUP" => 0,
"TABLE" => 0,
"TD" => 0,
"TEXTAREA" => 0,
"TFOOT" => 0,
"TH" => 0,
"THEAD" => 0,
"TITLE" => 0,
"TR" => 0,
"TT" => 0,
"U" => 0,
"UL" => 0,
"VAR" => 0);

$alltags = array(
"!--",
"!DOCTYPE",
"A", "ADDRESS", "APPLET", "AREA",
"B", "BASE", "BASEFONT", "BGSOUND", "BIG", "BLINK", "BLOCKQUOTE", "BODY",
"BR",
"CAPTION", "CENTER", "CITE", "CODE", "COL", "COLGROUP", "COMMENT",
"DD", "DFN", "DIR", "DIV", "DL", "DT", "DD", "DT",
"EM", "EMBED",
"FONT", "FORM", "FRAME", "FRAMESET",
"H1", "H2", "H3", "H4", "H5", "H6", "HEAD", "HR", "HTML",
"I", "IFRAME", "ILAYER", "IMG", "INPUT", "ISINDEX",
"KBD", "KEYGEN",
"LAYER", "LI", "LINK", "LISTING",
"MAP", "MARQUEE", "MENU", "META", "MULTICOL",
"NOBR", "NOEMBED", "NOFRAMES", "NOLAYER", "NOSCRIPT",
"OBJECT", "OL", "OPTION",
"P", "PARAM", "PLAINTEXT", "PRE",
"S", "STRIKE", "SAMP", "SCRIPT", "SELECT", "SELECT", "SERVER", "SMALL",
"SPACER",
"SPAN", "STRIKE", "STRONG", "STYLE", "SUB", "SUP",
"TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR",
"TT",
"U", "UL",
"VAR",
"WBR");

$require = array("THEAD" => "TABLE",
"TH" => "TABLE",
"TFOOT" => "TABLE",
"TBODY" => "TABLE",
"TD" => "TABLE",
"TR" => "TABLE",
"LI" => array("OL", "UL", "DIR", "MENU"),
"FRAME" => "FRAMSET",
"INPUT" => "FORM",
"SELECT" => "FORM",
"TEXTAREA" => "FORM",
"PARAM" => array("APPLET", "EMBED", "OBJECT"),
"WBR" => "NOBR",
"TITLE" => "HEAD",
"LINK" => "HEAD",
"OPTION" => "SELECT",
"DT" => "DL",
"DD" => "DL",
"HEAD" => "HTML",
"BODY" => "HTML");

$mal_place = array("THEAD" => 0,
"TH" => 0,
"TFOOT" => 0,
"TBODY" => 0,
"TD" => 0,
"TR" => 0,
"LI" => 0,
"FRAME" => 0,
"INPUT" => 0,
"SELECT" => 0,
"TEXTAREA" => 0,
"PARAM" => 0,
"WBR" => 0,
"TITLE" => 0,
"LINK" => 0,
"OPTION" => 0,
"DT" => 0,
"DD" => 0,
"HEAD" => 0,
"BODY" => 0);


$retour = array("ouvertures multiples" => array(),
"non ouverte" => $openclosetags,
"non fermee" => $openclosetags,
"mal placee" => $mal_place,
"mal emboitee" => array(),
"inconnue" => array());
$lines = @file($url);

if (!$lines)
return FALSE;

$file = implode(" ", $lines);

$file = preg_replace("/\s+/", " ", $file);
$file = preg_replace("/<!--[^>]+>/", "", $file);

preg_match_all("/<(\/?[^> ]+)[^>]*>/i", $file, $balises);
$pile_balise = array();
for ($i=0;$i<sizeof($balises[0]);$i++)
{
$balise = strtoupper($balises[1][$i]);
if (!in_array($balise, $alltags) && !in_array(substr($balise, 1, strlen(
$balise)), $alltags) && !in_array($balise, $retour["inconnue"]))
{
$retour["inconnue"][] = $balise;
}
else
{
/* Fermeture d'une balise */
if (substr($balise, 0, 1) == "/")
{
$balise = substr($balise, 1, strlen($balise));
if (isset($openclosetags[$balise]))
{
$openclosetags[$balise]--;
if (($parent = array_pop($pile_balise)) != $balise)
$retour["mal emboitee"][$balise] = $parent;
if ($openclosetags[$balise] < 0)
{
$openclosetags[$balise] = 0;
$retour["non ouverte"][$balise]++;
}
}
}
/* Ouverture d'une balise */
else
{
if (isset($require[$balise]))
{
if (is_array($require[$balise]))
{
$ok = FALSE;
$j=0;
while ($j<sizeof($require[$balise]) && !$ok)
{
if ($openclosetags[$require[$balise][$j]] > 0)
$ok = TRUE;
$j++;
}
if (!$ok)
{
$retour["mal placee"][$balise]++;
}
}
else
{
if ($openclosetags[$require[$balise]] <= 0)
{
$retour["mal placee"][$balise]++;
}
}
}
if (isset($openclosetags[$balise]))
{
if (!in_array($balise, $notonlyonce) && ($openclosetags[$balise] > 0) &&
!in_array($balise, $retour["ouvertures multiples"]))
{
$retour["ouvertures multiples"][] = $balise;
}
array_push($pile_balise, $balise);
$openclosetags[$balise]++;
}
}
}
}

foreach ($openclosetags as $key => $value)
{
if ($value > 0)
$retour["non fermee"][$key] = $value;
}
return $retour;
}
?>
<?php
/*------------------------------*/
/*
Titre : Vérifie la validité d'un code HTML

Date édition : 14 Fev 2019
Date mise a jour : 13 Aout 2019

Rapport de la maj:
- fonctionnement du code vérifié
- maintenance du code
*/
/*------------------------------*/

function html_check($url){
/* Parametre : $url : URL ou chemin d'acces du fichier a tester
* La variable retournée est 1 tableau associatif contenant des
tableaux:
* "ouvertures multiples" => tableau des balises concernées,

* "non ouverte" => tableau a 2 dimensions (balise -> nombre)
* "non fermee" => tableau a 2 dimensions (balise -> nombre)
* "mal placee" => tableau a 2 dimensions (balise -> nombre)
* "mal emboitee" => tableau a 2 dimensions
* (balise fermante -> balise ouvrante en conflit)
* "inconnue" => tableau des balises inconnues
*/

$notonlyonce = array("TABLE", "FRAMESET");
$openclosetags = array(
"A" => 0,
"ADDRESS" => 0,
"APPLET" => 0,
"B" => 0,
"BIG" => 0,
"BLINK" => 0,
"BLOCKQUOTE" => 0,
"BODY" => 0,
"CAPTION" => 0,
"CENTER" => 0,
"CITE" => 0,
"CODE" => 0,
"COMMENT" => 0,
"DD" => 0,
"DFN" => 0,
"DIR" => 0,
"DIV" => 0,
"DL" => 0,
"DT" => 0,
"DT" => 0,
"EM" => 0,
"EMBED" => 0,
"FONT" => 0,
"FORM" => 0,
"FRAME" => 0,
"FRAMESET" => 0,
"H1" => 0, "H2" => 0, "H3" => 0, "H4" => 0, "H5" => 0, "H6" => 0,
"HEAD" => 0,
"HTML" => 0,
"I" => 0,
"IFRAME" => 0,
"ILAYER" => 0,
"KBD" => 0,
"LAYER" => 0,
"LISTING" => 0,
"MAP" => 0,
"MARQUEE" => 0,
"MENU" => 0,
"MULTICOL" => 0,
"NOBR" => 0,
"NOEMBED" => 0,
"NOFRAMES" => 0,
"NOLAYER" => 0,
"NOSCRIPT" => 0,
"OBJECT" => 0,
"OL" => 0,
"OPTION" => 0,
"PARAM" => 0,
"PRE" => 0,
"S" => 0,
"SAMP" => 0,
"SCRIPT" => 0,
"SELECT" => 0,
"SERVER" => 0,
"SMALL" => 0,
"SPAN" => 0,
"STRIKE" => 0,
"STRONG" => 0,
"STYLE" => 0,
"SUB" => 0,
"SUP" => 0,
"TABLE" => 0,
"TD" => 0,
"TEXTAREA" => 0,
"TFOOT" => 0,
"TH" => 0,
"THEAD" => 0,
"TITLE" => 0,
"TR" => 0,
"TT" => 0,
"U" => 0,
"UL" => 0,
"VAR" => 0);

$alltags = array(
"!--",
"!DOCTYPE",
"A", "ADDRESS", "APPLET", "AREA",
"B", "BASE", "BASEFONT", "BGSOUND", "BIG", "BLINK", "BLOCKQUOTE", "BODY",
"BR",
"CAPTION", "CENTER", "CITE", "CODE", "COL", "COLGROUP", "COMMENT",
"DD", "DFN", "DIR", "DIV", "DL", "DT", "DD", "DT",
"EM", "EMBED",
"FONT", "FORM", "FRAME", "FRAMESET",
"H1", "H2", "H3", "H4", "H5", "H6", "HEAD", "HR", "HTML",
"I", "IFRAME", "ILAYER", "IMG", "INPUT", "ISINDEX",
"KBD", "KEYGEN",
"LAYER", "LI", "LINK", "LISTING",
"MAP", "MARQUEE", "MENU", "META", "MULTICOL",
"NOBR", "NOEMBED", "NOFRAMES", "NOLAYER", "NOSCRIPT",
"OBJECT", "OL", "OPTION",
"P", "PARAM", "PLAINTEXT", "PRE",
"S", "STRIKE", "SAMP", "SCRIPT", "SELECT", "SELECT", "SERVER", "SMALL",
"SPACER",
"SPAN", "STRIKE", "STRONG", "STYLE", "SUB", "SUP",
"TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR",
"TT",
"U", "UL",
"VAR",
"WBR");

$require = array("THEAD" => "TABLE",
"TH" => "TABLE",
"TFOOT" => "TABLE",
"TBODY" => "TABLE",
"TD" => "TABLE",
"TR" => "TABLE",
"LI" => array("OL", "UL", "DIR", "MENU"),
"FRAME" => "FRAMSET",
"INPUT" => "FORM",
"SELECT" => "FORM",
"TEXTAREA" => "FORM",
"PARAM" => array("APPLET", "EMBED", "OBJECT"),
"WBR" => "NOBR",
"TITLE" => "HEAD",
"LINK" => "HEAD",
"OPTION" => "SELECT",
"DT" => "DL",
"DD" => "DL",
"HEAD" => "HTML",
"BODY" => "HTML");

$mal_place = array("THEAD" => 0,
"TH" => 0,
"TFOOT" => 0,
"TBODY" => 0,
"TD" => 0,
"TR" => 0,
"LI" => 0,
"FRAME" => 0,
"INPUT" => 0,
"SELECT" => 0,
"TEXTAREA" => 0,
"PARAM" => 0,
"WBR" => 0,
"TITLE" => 0,
"LINK" => 0,
"OPTION" => 0,
"DT" => 0,
"DD" => 0,
"HEAD" => 0,
"BODY" => 0);


$retour = array("ouvertures multiples" => array(),
"non ouverte" => $openclosetags,
"non fermee" => $openclosetags,
"mal placee" => $mal_place,
"mal emboitee" => array(),
"inconnue" => array());
$lines = @file($url);

if (!$lines)
return FALSE;

$file = implode(" ", $lines);

$file = preg_replace("/\s+/", " ", $file);
$file = preg_replace("/<!--[^>]+>/", "", $file);

preg_match_all("/<(\/?[^> ]+)[^>]*>/i", $file, $balises);
$pile_balise = array();
for ($i=0;$i<sizeof($balises[0]);$i++)
{
$balise = strtoupper($balises[1][$i]);
if (!in_array($balise, $alltags) && !in_array(substr($balise, 1, strlen(
$balise)), $alltags) && !in_array($balise, $retour["inconnue"]))
{
$retour["inconnue"][] = $balise;
}
else
{
/* Fermeture d'une balise */
if (substr($balise, 0, 1) == "/")
{
$balise = substr($balise, 1, strlen($balise));
if (isset($openclosetags[$balise]))
{
$openclosetags[$balise]--;
if (($parent = array_pop($pile_balise)) != $balise)
$retour["mal emboitee"][$balise] = $parent;
if ($openclosetags[$balise] < 0)
{
$openclosetags[$balise] = 0;
$retour["non ouverte"][$balise]++;
}
}
}
/* Ouverture d'une balise */
else
{
if (isset($require[$balise]))
{
if (is_array($require[$balise]))
{
$ok = FALSE;
$j=0;
while ($j<sizeof($require[$balise]) && !$ok)
{
if ($openclosetags[$require[$balise][$j]] > 0)
$ok = TRUE;
$j++;
}
if (!$ok)
{
$retour["mal placee"][$balise]++;
}
}
else
{
if ($openclosetags[$require[$balise]] <= 0)
{
$retour["mal placee"][$balise]++;
}
}
}
if (isset($openclosetags[$balise]))
{
if (!in_array($balise, $notonlyonce) && ($openclosetags[$balise] > 0) &&
!in_array($balise, $retour["ouvertures multiples"]))
{
$retour["ouvertures multiples"][] = $balise;
}
array_push($pile_balise, $balise);
$openclosetags[$balise]++;
}
}
}
}

foreach ($openclosetags as $key => $value)
{
if ($value > 0)
$retour["non fermee"][$key] = $value;
}
return $retour;
}
?>

      Fonctions du code - Doc officielle PHP

   php.net  
Description
Versions PHP
    array
Crée un tableau
PHP 4, 5, 7 et 8
    array_pop
Dépile un élément de la fin d'un tableau
PHP 4, 5, 7 et 8
    array_push
Empile un ou plusieurs éléments à la fin d'un tableau
PHP 4, 5, 7 et 8
    file
Lit le fichier et renvoie le résultat dans un tableau
PHP 4, 5, 7 et 8
    implode
Rassemble les éléments d'un tableau en une chaîne
PHP 4, 5, 7 et 8
    in_array
Indique si une valeur appartient à un tableau
PHP 4, 5, 7 et 8
    isset
Détermine si une variable est déclarée et est différente de null
PHP 4, 5, 7 et 8
    is_array
Détermine si une variable est un tableau
PHP 4, 5, 7 et 8
    preg_match_all
Expression rationnelle globale
PHP 4, 5, 7 et 8
    preg_replace
Rechercher et remplacer par expression rationnelle standard
PHP 4, 5, 7 et 8
    return
Retourne le controle du programme au module appelant
PHP 4, 5, 7 et 8
    sizeof
Alias de count
PHP 4, 5, 7 et 8
    strlen
Calcule la taille d'une chaîne
PHP 4, 5, 7 et 8
    strtoupper
Renvoie une chaîne en majuscules
PHP 4, 5, 7 et 8
    substr
Retourne un segment de chaîne
PHP 4, 5, 7 et 8
Minimum 10 mots. Votre commentaire sera visible après validation.


 Autres snippets qui pourraient vous intéresser

Vérifier la validité et l'existence d'une date

Compatibilité : PHP 5, PHP 7, PHP 8

Codes pour vérifier si une date existe via la fonction checkdate () et une autre fonction pour vérifier si le format de date est bien valide.

Colorise du code PHP avec validation HTML

Compatibilité : PHP 5, PHP 7, PHP 8

Colorise du code PHP et utilise la fonction native de PHP : highlight_string (). Ce script permet de valider la colorisation avec le HTML.

* Requêtes exécutées avec Recherche Contextuelle

  14 Fév 2019

Information sur les mises à jour

Dernière mise à jour :

13 Août 2019
fonctionnement du code vérifié
maintenance du code

2 511 Vues
Compatibilité du code
PHP 5, PHP 7, PHP 8