Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 207 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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