Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 207 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
	}
821 mathilde 25
 
26
	function supprimerAccents($chaine){
27
		return strtr($chaine,array('à' => 'a','á' => 'a','â' => 'a','ã' => 'a','ä' => 'a',
28
											'ç' => 'c',
29
											'è' => 'e','é' => 'e','ê' => 'e','ë' => 'e',
30
											'ì' => 'i','í' => 'i','î' => 'i','ï' => 'i',
31
											'ñ' => 'n',
32
											'ò' => 'o', 'ó' => 'o' , 'ô' => 'o', 'õ' => 'o', 'ö' => 'o',
33
											'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
34
											'ý' => 'y', 'ÿ' => 'y'));
35
	}
137 jpm 36
 
37
	public function surlignerMots($mots) {
38
		$this->verifierTableauDeMots($mots);
39
		$this->texte = preg_replace_callback('`<[^>]+>`', array($this, 'sauverTags'), $this->texte);
40
		foreach ($mots as $mot) {
41
			$this->initialiserNbreSurlignageCourant();
42
			$this->texte = $this->surlignerMot($mot);
43
		}
44
		$this->texte = preg_replace_callback('`<([0-9]+)>`', array($this, 'restaurerTags'), $this->texte);
45
		return $this->texte;
46
	}
47
 
48
	private function verifierTableauDeMots($mots) {
49
		if (is_array($mots) === false) {
50
			$message = "Surligneur::surlignerMots() n'accepte que les tableaux de mots en argument";
51
			throw new InvalidArgumentException($message);
52
		} else {
53
			if (count($mots) == 0) {
54
				$message = "Surligneur::surlignerMots() n'accepte que des tableaux contenant au moins un mot";
55
				throw new LengthException($message);
56
			}
57
		}
58
	}
59
 
60
	private function sauverTags($match) {
61
		$i = count($this->tags);
62
		$this->tags[$i] = $match[0];
63
		return '<'.$i.'>';
64
	}
65
 
66
	private function initialiserNbreSurlignageCourant() {
67
		$this->nbreSurlignageCourant = 0;
68
	}
69
 
70
	private function surlignerMot($mot) {
207 delphine 71
		$positionDebutMot = stripos($this->texte, $mot);
137 jpm 72
		$longueurMot = strlen($mot);
73
		$surlignage = $this->texte;
74
		if ($positionDebutMot !== false) {
75
			$this->nbreSurlignageCourant++;
76
			if ($this->nbreSurlignageCourant <= $this->nbreSurlignageMaxParMot) {
77
				$debut = substr($this->texte, 0, $positionDebutMot);
78
				$milieu = substr($this->texte, $positionDebutMot, $longueurMot);
79
				$this->texte = substr($this->texte, $positionDebutMot + $longueurMot);
80
				$fin = $this->surlignerMot($mot);
81
				$surlignage = $debut.$this->sauverTagSurlignage($milieu).$fin;
82
			}
83
		}
84
		return $surlignage;
85
	}
86
 
87
	private function sauverTagSurlignage($motTrouve) {
88
		$i = count($this->tags);
89
		$this->tags[$i] = '<span class="surlignage">'.$motTrouve.'</span>';
90
		return '<'.$i.'>';
91
	}
202 delphine 92
 
137 jpm 93
	private function restaurerTags($match) {
94
		return $this->tags[$match[1]];
95
	}
96
}
97
?>