Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 137 | Rev 207 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 137 Rev 202
Line 1... Line 1...
1
<?php
1
<?php
2
class Surligneur {
2
class Surligneur {
3
	private $texte = '';
3
	private $texte = '';
-
 
4
	private $texteSansAccent = '';
4
	private $tags = array();
5
	private $tags = array();
5
	private $nbreSurlignageMaxParMot = 1;
6
	private $nbreSurlignageMaxParMot = 1;
6
	private $nbreSurlignageCourant = 0;
7
	private $nbreSurlignageCourant = 0;
Line 7... Line 8...
7
 
8
 
8
	public function __construct($texte = null, $surlignageMaxParMot = null) {
9
	public function __construct($texte = null, $surlignageMaxParMot = null, ChaineManipulateur $manipulateurDeChaine = null) {
9
		if (is_null($texte) == false) {
10
		if (is_null($texte) == false) {
10
			$this->setTexte($texte);
11
			$this->setTexte($texte);
11
		}
12
		}
12
		if (is_null($surlignageMaxParMot) == false) {
13
		if (is_null($surlignageMaxParMot) == false) {
13
			$this->setNbreMaxSurlignageParMot($surlignageMaxParMot);
14
			$this->setNbreMaxSurlignageParMot($surlignageMaxParMot);
-
 
15
		}
-
 
16
		
14
		}
17
		$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
Line 15... Line 18...
15
	}
18
	}
16
 
19
 
17
	public function setTexte($txt) {
20
	public function setTexte($txt) {
Line 23... Line 26...
23
	}
26
	}
Line 24... Line 27...
24
 
27
 
25
	public function surlignerMots($mots) {
28
	public function surlignerMots($mots) {
26
		$this->verifierTableauDeMots($mots);
29
		$this->verifierTableauDeMots($mots);
-
 
30
		$this->texte = preg_replace_callback('`<[^>]+>`', array($this, 'sauverTags'), $this->texte);
27
		$this->texte = preg_replace_callback('`<[^>]+>`', array($this, 'sauverTags'), $this->texte);
31
		$this->texteSansAccent = $this->manipulateurDeChaine->supprimerAccents($this->texte);
28
		foreach ($mots as $mot) {
32
		foreach ($mots as $mot) {
29
			$this->initialiserNbreSurlignageCourant();
33
			$this->initialiserNbreSurlignageCourant();
30
			$this->texte = $this->surlignerMot($mot);
34
			$this->texte = $this->surlignerMot($mot);
31
		}
35
		}
Line 54... Line 58...
54
	private function initialiserNbreSurlignageCourant() {
58
	private function initialiserNbreSurlignageCourant() {
55
		$this->nbreSurlignageCourant = 0;
59
		$this->nbreSurlignageCourant = 0;
56
	}
60
	}
Line 57... Line 61...
57
 
61
 
58
	private function surlignerMot($mot) {
62
	private function surlignerMot($mot) {
59
		$positionDebutMot = stripos($this->texte, $mot);
63
		$positionDebutMot = stripos($this->texteSansAccent, $mot);
60
		$longueurMot = strlen($mot);
64
		$longueurMot = strlen($mot);
61
		$surlignage = $this->texte;
65
		$surlignage = $this->texte;
62
		if ($positionDebutMot !== false) {
66
		if ($positionDebutMot !== false) {
63
			$this->nbreSurlignageCourant++;
67
			$this->nbreSurlignageCourant++;
Line 75... Line 79...
75
	private function sauverTagSurlignage($motTrouve) {
79
	private function sauverTagSurlignage($motTrouve) {
76
		$i = count($this->tags);
80
		$i = count($this->tags);
77
		$this->tags[$i] = '<span class="surlignage">'.$motTrouve.'</span>';
81
		$this->tags[$i] = '<span class="surlignage">'.$motTrouve.'</span>';
78
		return '<'.$i.'>';
82
		return '<'.$i.'>';
79
	}
83
	}
80
 
84
	
81
	private function restaurerTags($match) {
85
	private function restaurerTags($match) {
82
		return $this->tags[$match[1]];
86
		return $this->tags[$match[1]];
83
	}
87
	}
84
}
88
}
85
?>
89
?>
86
90