137 |
jpm |
1 |
<?php
|
|
|
2 |
class Surligneur {
|
|
|
3 |
private $texte = '';
|
|
|
4 |
private $tags = array();
|
|
|
5 |
private $nbreSurlignageMaxParMot = 1;
|
|
|
6 |
private $nbreSurlignageCourant = 0;
|
|
|
7 |
|
|
|
8 |
public function __construct($texte = null, $surlignageMaxParMot = null) {
|
|
|
9 |
if (is_null($texte) == false) {
|
|
|
10 |
$this->setTexte($texte);
|
|
|
11 |
}
|
|
|
12 |
if (is_null($surlignageMaxParMot) == false) {
|
|
|
13 |
$this->setNbreMaxSurlignageParMot($surlignageMaxParMot);
|
|
|
14 |
}
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
public function setTexte($txt) {
|
|
|
18 |
$this->texte = $txt;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
public function setNbreMaxSurlignageParMot($nbre) {
|
|
|
22 |
$this->nbreSurlignageMaxParMot = $nbre;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public function surlignerMots($mots) {
|
|
|
26 |
$this->verifierTableauDeMots($mots);
|
|
|
27 |
$this->texte = preg_replace_callback('`<[^>]+>`', array($this, 'sauverTags'), $this->texte);
|
|
|
28 |
foreach ($mots as $mot) {
|
|
|
29 |
$this->initialiserNbreSurlignageCourant();
|
|
|
30 |
$this->texte = $this->surlignerMot($mot);
|
|
|
31 |
}
|
|
|
32 |
$this->texte = preg_replace_callback('`<([0-9]+)>`', array($this, 'restaurerTags'), $this->texte);
|
|
|
33 |
return $this->texte;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
private function verifierTableauDeMots($mots) {
|
|
|
37 |
if (is_array($mots) === false) {
|
|
|
38 |
$message = "Surligneur::surlignerMots() n'accepte que les tableaux de mots en argument";
|
|
|
39 |
throw new InvalidArgumentException($message);
|
|
|
40 |
} else {
|
|
|
41 |
if (count($mots) == 0) {
|
|
|
42 |
$message = "Surligneur::surlignerMots() n'accepte que des tableaux contenant au moins un mot";
|
|
|
43 |
throw new LengthException($message);
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
private function sauverTags($match) {
|
|
|
49 |
$i = count($this->tags);
|
|
|
50 |
$this->tags[$i] = $match[0];
|
|
|
51 |
return '<'.$i.'>';
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private function initialiserNbreSurlignageCourant() {
|
|
|
55 |
$this->nbreSurlignageCourant = 0;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
private function surlignerMot($mot) {
|
|
|
59 |
$positionDebutMot = stripos($this->texte, $mot);
|
|
|
60 |
$longueurMot = strlen($mot);
|
|
|
61 |
$surlignage = $this->texte;
|
|
|
62 |
if ($positionDebutMot !== false) {
|
|
|
63 |
$this->nbreSurlignageCourant++;
|
|
|
64 |
if ($this->nbreSurlignageCourant <= $this->nbreSurlignageMaxParMot) {
|
|
|
65 |
$debut = substr($this->texte, 0, $positionDebutMot);
|
|
|
66 |
$milieu = substr($this->texte, $positionDebutMot, $longueurMot);
|
|
|
67 |
$this->texte = substr($this->texte, $positionDebutMot + $longueurMot);
|
|
|
68 |
$fin = $this->surlignerMot($mot);
|
|
|
69 |
$surlignage = $debut.$this->sauverTagSurlignage($milieu).$fin;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
return $surlignage;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
private function sauverTagSurlignage($motTrouve) {
|
|
|
76 |
$i = count($this->tags);
|
|
|
77 |
$this->tags[$i] = '<span class="surlignage">'.$motTrouve.'</span>';
|
|
|
78 |
return '<'.$i.'>';
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private function restaurerTags($match) {
|
|
|
82 |
return $this->tags[$match[1]];
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
?>
|