alphanormalize_php  v.03.00.03 — June 17, 2020
alphanormalize-test.php
Go to the documentation of this file.
1 <?php // -*- coding: utf-8 -*-
2 
3 /** \file alphanormalize-test.php
4  * (June 17, 2020)
5  *
6  * \brief PHP/HTML page to test alphanormalize_php.
7  */
8 
9 ini_set('display_errors', 'stdout');
10 ini_set('display_startup_errors', 1);
11 ini_set('html_errors', 1);
12 
13 error_reporting(-1);
14 
15 assert_options(ASSERT_ACTIVE, true);
16 assert_options(ASSERT_WARNING, true);
17 assert_options(ASSERT_BAIL, true);
18 
19 mb_internal_encoding('UTF-8');
20 
21 require_once 'alphanormalize.inc';
22 
23 require_once 'html_entities_entitydefs.inc';
24 require_once 'html_entities_name2codepoint.inc';
25 require_once 'html_entities_codepoint2name.inc';
26 
27 ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
29  <head>
30  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
31  <meta http-equiv="Reply-To" content="olivier.pirson.opi@gmail.com" />
32 
33  <meta name="viewport" content="width=device-width, initial-scale=1" />
34 
35  <title>alphanormalize_php &ndash; tests</title>
36 
37  <meta name="copyright" content="&copy; Olivier Pirson" />
38  <meta name="author" content="Olivier Pirson" />
39  <meta name="date-creation-yyyymmdd" content="20101005" />
40  <meta name="date-revision-yyyymmdd" content="20200617" />
41  <meta name="description" content="Simple functions to remove accents and replace non-alphanumeric characters." />
42  <meta name="keywords" content="PHP, character, accents, Greek" />
43 
44  <style type="text/css">
45 h1 {
46  margin: 1ex 0 0;
47  text-align:center;
48 }
49 
50 table { border-collapse: collapse; }
51 
52 tr { vertical-align: top; }
53 
54 tr.sep { border-top:solid 3px silver; }
55 
56 th {
57  font-family: Dina, monospace;
58  font-weight: normal;
59  text-align: left;
60 }
61 
62 th,
63 td {
64  border: solid 1px silver;
65  white-space: nowrap;
66 }
67 td.sep { background-color: silver; }
68 
69 hr {
70  border-style: none;
71  border-top: dotted 1px silver;
72 }
73 
74 a.mail:link {
75  background-color: inherit;
76  color: green;
77 }
78 a.mail:visited {
79  background-color: inherit;
80  color: olive;
81 }
82 
83 div { margin-top: 3ex; }
84 
85 pre { margin: 0; }
86 
87 .center { text-align: center; }
88 
89 .smallskip { margin-top: 1ex; }
90 .bigskip { margin-top: 3ex; }
91 .bigbigskip { margin-top: 6ex; }
92 .bigbigbigskip { margin-top: 9ex; }
93 
94 .green { color: green; }
95 .red { color: red; }
96 
97 .surname { font-variant: small-caps; }
98  </style>
99  </head>
100  <body>
101  <h1><a rel="nofollow" href="https://bitbucket.org/OPiMedia/alphanormalize_php" style="text-decoration:none"><img src="https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2020/Jun/12/1411058763-4-alphanormalize_php-logo_avatar.png" width="48" height="48" alt="" style="border:none; vertical-align:top" />&nbsp;</a><a href="https://bitbucket.org/OPiMedia/alphanormalize_php"><tt>alphanormalize_php</tt></a> &ndash; tests</h1>
102  <div class="smallskip center">
103  <a rel="nofollow" lang="fr" href="http://www.opimedia.be/" style="text-decoration:none"><img src="http://www.opimedia.be/_png/OPi.png" width="41" height="34" alt="" style="border:none; vertical-align:middle" />&nbsp;</a><a lang="fr" href="http://www.opimedia.be/">Olivier <span class="surname">Pirson</span></a>
104  &ndash;
105  <a class="mail" href="mailto:olivier.pirson.opi@gmail.com?subject=[alphanormalize_php]"><tt>olivier.pirson.opi@gmail.com</tt></a>
106  &ndash;
107  v.<?php echo preg_replace('/---/', '&ndash;', \Alphanormalize\version()); ?>
108  </div>
109  <div class="smallskip center">
110  <?php echo 'PHP version ', PHP_VERSION; ?>
111  </div>
112  <ul>
113  <li>Sources on Bitbucket: <a href="https://bitbucket.org/OPiMedia/alphanormalize_php"><tt>https://bitbucket.org/OPiMedia/alphanormalize_php</tt></a></li>
114  <li><a href="http://www.opimedia.be/DS/webdev/PHP/alphanormalize-php/docs/">Online HTML documentation</a></li>
115  <li><a href="http://www.opimedia.be/DS/webdev/PHP/alphanormalize-php/alphanormalize-test.php">Online simple test page</a></li>
116  </ul>
117 
118  <div class="bigskip">
119 <?php
120 
121 $s = '<pre>Il était une fois&hellip; des <em>caractères</em> "bizarroïdes" (avec accent, cédille et compagnie, ou α, β, etc.)
122 accompagnés ou pas de formatage <em>(X)HTML</em>.
123 Je cherchais à m&rsquo;en débarrasser&nbsp;!
124 Ceci est ma solution.</pre>';
125 
126 ?>
127  <table>
128  <tr>
129  <th>$s</th>
130  <td class="sep"></td>
131  <td><?php echo $s; ?></td>
132  </tr>
133 
134  <tr class="sep">
136  <td class="sep"></td>
138  </tr>
139  <tr>
140  <th>mb_str_greek_to_alpha($s)</th>
141  <td class="sep"></td>
143  </tr>
144 
145  <tr class="sep">
146  <th>htmlspecialchars($s)</th>
147  <td class="sep"></td>
148  <td><?php echo htmlspecialchars($s); ?></td>
149  </tr>
150  <tr>
151  <th><strong>mb_str_alphanormalize($s)</strong></th>
152  <td class="sep"></td>
154  </tr>
155  <tr>
156  <th><strong>mb_str_alphanormalize($s, true)</strong></th>
157  <td class="sep"></td>
158  <td><?php echo \Alphanormalize\mb_str_alphanormalize($s, true); ?></td>
159  </tr>
160  </table>
161  </div>
162 
163  <div>
164  <tt>$ACCENTALPHA_TO_ALPHA[]</tt>:
165  <table>
166 <?php
167 
168 require 'accentalpha_to_alpha.inc';
169 
170 echo ' <tr>
171  <th>$c</th>
172 ';
173 
174 foreach ($ACCENTALPHA_TO_ALPHA as $c=>$v) {
175  if (in_array($c, array('À', 'ß', 'à', 'Œ'), true)) {
176  echo '<td class="sep"></td>
177 ';
178  }
179 
180  echo '<td>', $c, '</td>
181 ';
182 }
183 
184 echo ' </tr>
185  <tr class="sep">
186  <th>$ACCENTALPHA_TO_ALPHA[$c]</th>
187 ';
188 
189 foreach ($ACCENTALPHA_TO_ALPHA as $c=>$v) {
190  if (in_array($c, array('À', 'ß', 'à', 'Œ'), true)) {
191  echo '<td class="sep"></td>
192 ';
193  }
194 
195  echo '<td>', $v, '</td>
196 ';
197 }
198 
199 echo ' </tr>
200 ';
201 
202 ?>
203  </table>
204  </div>
205 
206  <div>
207  <tt>$GREEK_TO_ALPHA[]</tt>
208  (Adopts the standard <acronym>ONU</acronym>/<acronym>ELOT</acronym>:
209  see. <i>Memento <a href="http://www.opimedia.be/DS/mementos/grecs.htm">grecs</a></i>):
210  <table>
211 <?php
212 
213 require 'greek_to_alpha.inc';
214 
215 echo ' <tr>
216  <th>$c</th>
217 ';
218 
219 foreach ($GREEK_TO_ALPHA as $c=>$v) {
220  if (in_array($c, array('Α', 'α', 'ς', 'σ', 'ϑ'), true)) {
221  echo '<td class="sep"></td>
222 ';
223  }
224 
225  echo '<td>', $c, '</td>
226 ';
227 }
228 
229 echo ' </tr>
230  <tr class="sep">
231  <th>$GREEK_TO_ALPHA[$c]</th>
232 ';
233 
234 foreach ($GREEK_TO_ALPHA as $c=>$v) {
235  if (in_array($c, array('Α', 'α', 'ς', 'σ', 'ϑ'), true)) {
236  echo '<td class="sep"></td>
237 ';
238  }
239 
240  echo '<td>', $v, '</td>
241 ';
242 }
243 
244 echo ' </tr>
245 ';
246 
247 ?>
248  </table>
249  </div>
250 
251  <div class="bigbigbigskip">
252  Associative table returned by the PHP function
253  <tt>get_html_translation_table(HTML_SPECIALCHARS)</tt>:
254  <table>
255 <?php
256 
257 echo ' <tr>
258  <th>htmlspecialchars($c)</th>
259  <td class="sep"></td>
260 ';
261 
262 foreach (get_html_translation_table(HTML_SPECIALCHARS) as $c=>$v) {
263  echo '<td>', htmlspecialchars($c), '</td>
264 ';
265 }
266 
267 echo ' </tr>
268  <tr class="sep">
269  <th>htmlspecialchars(get_html_translation_table(HTML_SPECIALCHARS)[$c])</th>
270  <td class="sep"></td>
271 ';
272 
273 foreach (get_html_translation_table(HTML_SPECIALCHARS) as $c=>$v) {
274  echo '<td>', htmlspecialchars($v), '</td>
275 ';
276 }
277 
278 echo ' </tr>
279  <tr>
280  <th>mb_str_alphanormalize($c)</th>
281  <td class="sep"></td>
282 ';
283 
284 foreach (get_html_translation_table(HTML_SPECIALCHARS) as $c=>$v) {
285  echo '<td>', \Alphanormalize\mb_str_alphanormalize($c), '</td>
286 ';
287 }
288 
289 echo ' </tr>
290 ';
291 
292 ?>
293  </table>
294  </div>
295 
296  <div>
297  Associative table returned by the PHP function
298  <tt>get_html_translation_table(HTML_ENTITIES)</tt>:
299  <table>
300 <?php
301 
302 echo ' <tr>
303  <th>htmlspecialchars(utf8_encode($c))</th>
304  <td class="sep"></td>
305 ';
306 
307 foreach (get_html_translation_table(HTML_ENTITIES) as $c=>$v) {
308  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
309  $c = utf8_encode($c);
310  }
311  echo '<td', (in_array($c, $HTML_ENTITIES_ENTITYDEFS, true)
312  ? '>'
313  : ' class="red">'), htmlspecialchars($c), '</td>
314 ';
315 }
316 
317 echo ' </tr>
318  <tr class="sep">
319  <th>htmlspecialchars(get_html_translation_table(HTML_ENTITIES)[$c])</th>
320  <td class="sep"></td>
321 ';
322 
323 foreach (get_html_translation_table(HTML_ENTITIES) as $c=>$v) {
324  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
325  $c = utf8_encode($c);
326  }
327  echo '<td', (in_array($c, $HTML_ENTITIES_ENTITYDEFS, true)
328  ? '>'
329  : ' class="red">'), htmlspecialchars($v), '</td>
330 ';
331 }
332 
333 echo ' </tr>
334  <tr>
335  <th>mb_str_alphanormalize(utf8_encode($c))</th>
336  <td class="sep"></td>
337 ';
338 
339 foreach (get_html_translation_table(HTML_ENTITIES) as $c=>$v) {
340  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
341  $c = utf8_encode($c);
342  }
343  echo '<td', (in_array($c, $HTML_ENTITIES_ENTITYDEFS, true)
344  ? '>'
345  : ' class="red">'), \Alphanormalize\mb_str_alphanormalize($c), '</td>
346 ';
347 }
348 
349 echo ' </tr>
350 ';
351 
352 ?>
353  </table>
354  (<?php echo count(get_html_translation_table(HTML_ENTITIES)); ?> éléments)
355  </div>
356 
357  <div class="bigbigbigskip">
358  Three associative tables (copied from Python dictionary of module
359  <a href="http://docs.python.org/py3k/library/html.entities.html#module-html.entities"><tt>html.entities</tt></a>):<br />
360  <tt>$HTML_ENTITIES_ENTITYDEFS[]</tt>:
361  <table>
362 <?php
363 
364 $array_get_html_translation_table = get_html_translation_table(HTML_ENTITIES);
365 
366 echo ' <tr>
367  <th>$k</th>
368  <td class="sep"></td>
369 ';
370 
371 foreach ($HTML_ENTITIES_ENTITYDEFS as $k=>$v) {
372  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
373  $v = utf8_decode($v);
374  }
375 
376  echo '<td', (array_key_exists($v, $array_get_html_translation_table)
377  ? '>'
378  : ' class="green">'), $k, '</td>
379 ';
380 }
381 
382 echo ' </tr>
383  <tr class="sep">
384  <th>htmlspecialchars($HTML_ENTITIES_ENTITYDEFS[$k])</th>
385  <td class="sep"></td>
386 ';
388 
389 foreach ($HTML_ENTITIES_ENTITYDEFS as $k=>$v) {
390  $dv = (version_compare(PHP_VERSION, '5.4.0') < 0
391  ? utf8_decode($v)
392  : $v);
393 
394  if (!array_key_exists($dv, $array_get_html_translation_table)) {
395  $nb_diff++;
396  }
397  echo '<td', (array_key_exists($dv, $array_get_html_translation_table)
398  ? '>'
399  : ' class="green">'), htmlspecialchars($v), '</td>
400 ';
401 }
402 
403 echo ' </tr>
404 ';
405 
406 ?>
407  </table>
408  (In <span class="green">green</span> the <?php echo $nb_diff; ?> items over <?php echo count($HTML_ENTITIES_ENTITYDEFS); ?>
409  which are not in <tt>get_html_translation_table(HTML_ENTITIES)</tt>.
410  Previous PHP 5.4, all HTML entities are not supported!)
411  </div>
412 
413  <div>
415  <table>
416 <?php
417 
418 echo ' <tr>
419  <th>$k</th>
420  <td class="sep"></td>
421 ';
422 
423 foreach ($HTML_ENTITIES_NAME2CODEPOINT as $k=>$v) {
424  $c = $HTML_ENTITIES_ENTITYDEFS[$k];
425  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
426  $c = utf8_decode($c);
427  }
428 
429  echo '<td', (array_key_exists($c, $array_get_html_translation_table)
430  ? '>'
431  : ' class="green">'), $k, '</td>
432 ';
433 }
434 
435 echo ' </tr>
436  <tr class="sep">
437  <th>$HTML_ENTITIES_NAME2CODEPOINT[$k]</th>
438  <td class="sep"></td>
439 ';
440 
441 foreach ($HTML_ENTITIES_NAME2CODEPOINT as $k=>$v) {
442  $c = $HTML_ENTITIES_ENTITYDEFS[$k];
443  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
444  $c = utf8_decode($c);
445  }
446 
447  echo '<td', (array_key_exists($c, $array_get_html_translation_table)
448  ? '>'
449  : ' class="green">'), $v, '</td>
450 ';
451 }
452 
453 echo ' </tr>
454 ';
455 
456 ?>
457  </table>
458  </div>
459 
460  <div>
462  <table>
463 <?php
464 
465 echo ' <tr>
466  <th>$k</th>
467  <td class="sep"></td>
468 ';
469 
470 foreach ($HTML_ENTITIES_CODEPOINT2NAME as $k=>$v) {
471  if ($k === 338) {
472  echo '<td class="sep"></td>
473 ';
474  }
475 
476  $c = $HTML_ENTITIES_ENTITYDEFS[$v];
477  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
478  $c = utf8_decode($c);
479  }
480 
481  echo '<td', (array_key_exists($c, $array_get_html_translation_table)
482  ? '>'
483  : ' class="green">'), $k, '</td>
484 ';
485 }
486 
487 echo ' </tr>
488  <tr class="sep">
489  <th>$HTML_ENTITIES_CODEPOINT2NAME[$k]</th>
490  <td class="sep"></td>
491 ';
492 
493 foreach ($HTML_ENTITIES_CODEPOINT2NAME as $k=>$v) {
494  if ($k === 338) {
495  echo '<td class="sep"></td>
496 ';
497  }
498 
499  $c = $HTML_ENTITIES_ENTITYDEFS[$v];
500  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
501  $c = utf8_decode($c);
502  }
503 
504  echo '<td', (array_key_exists($c, $array_get_html_translation_table)
505  ? '>'
506  : ' class="green">'), $v, '</td>
507 ';
508 }
509 
510 echo ' </tr>
511 ';
512 
513 ?>
514  </table>
515  </div>
516 
517 <?php
518 
519 /* Tests */
520 foreach (get_html_translation_table(HTML_ENTITIES) as $c=>$v) {
521  if (version_compare(PHP_VERSION, '5.4.0') < 0) {
522  $c = utf8_encode($c);
523  }
524  $v = mb_substr($v, 1, mb_strlen($v) - 2);
525  assert($HTML_ENTITIES_ENTITYDEFS[$v] === $c);
526 }
527 
528 foreach ($HTML_ENTITIES_NAME2CODEPOINT as $k=>$v) {
530 }
531 
532 foreach ($HTML_ENTITIES_CODEPOINT2NAME as $k=>$v) {
534 }
535 
536 ?>
537  </body>
538 </html>
mb_str_accentalpha_to_alpha($s, $encoding=null)
Copy of $s without "accents".
$GREEK_TO_ALPHA
mb_str_greek_to_alpha($s, $encoding=null)
Copy of $s with the characters of the Greek alphabet were replaced.
version()
Return the version of this module.
$array_get_html_translation_table
mb_str_alphanormalize($s, $strip=false, $entity_decode=false, $replacement='_', $encoding=null)
Copy of $s without "accents" with the characters of the Greek alphabet were replaced and all non-alph...
$ACCENTALPHA_TO_ALPHA