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