Précédent   Forum des professionnels en informatique > PHP > Langage > Regex
Regex Forum d'entraide sur les expressions rationnelles PHP. Avant de poster -> FAQ regex, Cours de regex et Sources de regex
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 20/04/2011, 17h02   #1
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Par défaut Fonction découpe texte qui garde en forme

Bonjour,

Depuis pas mal de temps, je cherche à couper le texte HTML en gardant la mise en forme de celui-ci. En effet, j'écris des récits d'aventures qui font plusieurs milliers de mots. Pour le lecteur, j'ai préféré découper tout cela en page. Je me suis donc mis en quête d'une fonction. Après avoir trouvé et modifié une fonction, je suis arrivé à quelque chose de pas mal et qui fonctionne : Exemple de récit d'aventure : http://partir-en-vtt.com/periples_vt...r_periples=165

Le découpage permet d'alimenter une table stockant les caractères de la page 1,2,3...avec l'id du bon périple.

Il réside encore un souci (sinon je ne posterai pas !). En effet, parfois, le découpage coupe au milieu d'un mot et je ne trouve pas cela terrible.

Voici le code de la fonction couper_texte :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?/**
 * Coupe une chaine en gardant le formatage HTML
 * @param string $text Texte à couper
 * @param integer $length Longueur à garder
 * @param string $ending Caractères à ajouter à la fin
 * @param boolean $exact Coupure exacte
 * @return string
 */
function trunchtml($text, $length, $ending = '...', $exact = false) {
    if(strlen($text)< $length)
	{
		$truncate = $text;
	}
	else
	{
 
		if(strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
			return $text;
		}
		preg_match_all('/(<.+?>)?([^<>]*)/is', $text, $matches, PREG_SET_ORDER);
		$total_length = 0;
		$arr_elements = array();
		$truncate = '';
		foreach($matches as $element) {
			if(!empty($element[1])) 
			{
				if(preg_match('/^<\s*.+?\/\s*>$/s', $element[1])) 
				{
 
				}
				else if(preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $element[1], $element2)) 
				{
					$pos = array_search($element2[1], $arr_elements);
					if($pos !== false) 
					{
						unset($arr_elements[$pos]);
					}
				} 
				else if(preg_match('/^<\s*([^\s>!]+).*?>$/s', $element[1], $element2)) 
				{
					array_unshift($arr_elements,
					strtolower($element2[1]));
				}
				$truncate .= $element[1];
			}
			$content_length = strlen(preg_replace('/(&[a-z]{1,6};|&#[0-9]+;)/i', ' ', $element[2]));
 
			if($total_length >= $length) 
			{
				break;
			} 
			elseif ($total_length+$content_length > $length) 
			{
				$left = $total_length>$length?$total_length-$length:$length-$total_length;
				$entities_length = 0;
				if(preg_match_all('/&[a-z]{1,6};|&#[0-9]+;/i', $element[2], $element3, PREG_OFFSET_CAPTURE)) 
				{
					foreach($element3[0] as $entity) 
					{
						if($entity[1]+1-$entities_length <= $left) 
						{
							$left--;
							$entities_length += strlen($entity[0]);
						} else break;
					}
				}
				$truncate .= substr($element[2], 0, $left+$entities_length);
				break;
			} 
			else 
			{
				$truncate .= $element[2];
				$total_length += $content_length;
			}
		}
		if(!$exact) 
		{
			$spacepos = strrpos($truncate, ' ');
			if(isset($spacepos)) 
			{
				$truncate = substr($truncate, 0, $spacepos);
			}
		}
		$truncate .= $ending;
		foreach($arr_elements as $element) 
		{
			$truncate .= '</' . $element . '>';
		}
	}
    return strlen($truncate);
}
?>
Et voilà comment j'exploite cette fonction :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	//calcul nb_page
			$nb_page = 0;
			$pages = array();
			//appel de couper_texte
 
			include($racine . '/inclusion/couper_texte_old.php');
			$nombreDeCaractParPage = intval(7000);
			$reste_contenu = $contenu;
 
			$indice_coupe = 7000;
			$cumul_coupe = 0;
 
			while ($indice_coupe >= $nombreDeCaractParPage )
			{
				$cumul_coupe += $indice_coupe;
				$indice_coupe = trunchtml($reste_contenu,$nombreDeCaractParPage);
				//echo $nb_page.'----';
				//$pages[$nb_page]['indice'] = $indice_coupe;
				//$pages[$nb_page]['cumul'] = $cumul_coupe;
				//$pages[$nb_page]['texte'] = substr($reste_contenu,0,$indice_coupe);;
 
				$nb_page++;
				$reste_contenu = substr($reste_contenu,$indice_coupe);
			}
Le code est un peu long mais j'espère que vous arriverez à m'aider et qu'il aidera quelques personnes cherchant cette fonction.

Par avance merci.
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/04/2011, 01h00   #2
Membre du Club
 
Avatar de betadev
 
Homme
Développeur Web
Inscription : octobre 2008
Messages : 80
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 27
Localisation : Tunisie

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : octobre 2008
Messages : 80
Points : 56
Points : 56
Salut ,

C'est très intéressante cette truc , j'ai déjà réfléchi à chercher une solution car j'été obligé pas mal de fois de supprimer les balises html pour prendre une portion d'un texte mis en forme.

Mais , pour fonction "trunchtml" elle ne fonctionne pas correctement , car j'ai effectué un test simple ci dessous et elle a donné un résultat mal regarde :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 
function trunchtml($text, $length, $ending = '...', $exact = false) {
    if(strlen($text)< $length)
	{
		$truncate = $text;
	}
	else
	{
 
		if(strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
			return $text;
		}
		preg_match_all('/(<.+?>)?([^<>]*)/is', $text, $matches, PREG_SET_ORDER);
		$total_length = 0;
		$arr_elements = array();
		$truncate = '';
		foreach($matches as $element) {
			if(!empty($element[1])) 
			{
				if(preg_match('/^<\s*.+?\/\s*>$/s', $element[1])) 
				{
 
				}
				else if(preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $element[1], $element2)) 
				{
					$pos = array_search($element2[1], $arr_elements);
					if($pos !== false) 
					{
						unset($arr_elements[$pos]);
					}
				} 
				else if(preg_match('/^<\s*([^\s>!]+).*?>$/s', $element[1], $element2)) 
				{
					array_unshift($arr_elements,
					strtolower($element2[1]));
				}
				$truncate .= $element[1];
			}
			$content_length = strlen(preg_replace('/(&[a-z]{1,6};|&#[0-9]+;)/i', ' ', $element[2]));
 
			if($total_length >= $length) 
			{
				break;
			} 
			elseif ($total_length+$content_length > $length) 
			{
				$left = $total_length>$length?$total_length-$length:$length-$total_length;
				$entities_length = 0;
				if(preg_match_all('/&[a-z]{1,6};|&#[0-9]+;/i', $element[2], $element3, PREG_OFFSET_CAPTURE)) 
				{
					foreach($element3[0] as $entity) 
					{
						if($entity[1]+1-$entities_length <= $left) 
						{
							$left--;
							$entities_length += strlen($entity[0]);
						} else break;
					}
				}
				$truncate .= substr($element[2], 0, $left+$entities_length);
				break;
			} 
			else 
			{
				$truncate .= $element[2];
				$total_length += $content_length;
			}
		}
		if(!$exact) 
		{
			$spacepos = strrpos($truncate, ' ');
			if(isset($spacepos)) 
			{
				$truncate = substr($truncate, 0, $spacepos);
			}
		}
		$truncate .= $ending;
		foreach($arr_elements as $element) 
		{
			$truncate .= '</' . $element . '>';
		}
	}
    return strlen($truncate);
}
#######
$chaine = "<strong>he<em>ll</em>o</strong>" ;			
$indice_coupe = trunchtml($chaine,3);		
print substr($chaine,0,$indice_coupe) ;
résultat dans le code source de la page est :
ce qui est érroné comme html
betadev est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/04/2011, 01h11   #3
Membre du Club
 
Avatar de betadev
 
Homme
Développeur Web
Inscription : octobre 2008
Messages : 80
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 27
Localisation : Tunisie

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : octobre 2008
Messages : 80
Points : 56
Points : 56
J'ai effectué une recherche sur le net et je suis tombé sur une fonction qui fonctionne correctement (avec un exemple)


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
 
/*                                                                            */
/* Titre          : Tronquer une chaîne de caractères HTML                    */
/*                                                                            */
 
/* Auteur         : forty                                                     */
/* Date édition   : 21 Mai 2008                                               */
/*                                                                            */
 
 
 
/*
 * Script base sur le parser html disponible ici :
 http://php-html.sourceforge.net/
 */
 
define ("NODE_TYPE_START",0);
define ("NODE_TYPE_ELEMENT",1);
define ("NODE_TYPE_ENDELEMENT",2);
define ("NODE_TYPE_TEXT",3);
define ("NODE_TYPE_COMMENT",4);
define ("NODE_TYPE_DONE",5);
define ("NODE_TYPE_ELEMENT_END",6);
 
/**
 * Class HtmlParser.
 * To use, create an instance of the class passing
 * HTML text. Then invoke parse() until it's false.
 * When parse() returns true, $iNodeType, $iNodeName
 * $iNodeValue and $iNodeAttributes are updated.
 *
 * To create an HtmlParser instance you may also
 * use convenience functions HtmlParser_ForFile
 * and HtmlParser_ForURL.
 */
class HtmlParser {
 
    /**
    * Field iNodeType.
    * May be one of the NODE_TYPE_* constants above.
    */
    var $iNodeType;
 
    /**
    * Field iNodeName.
    * For elements, it's the name of the element.
    */
    var $iNodeName = "";
 
    /**
    * Field iNodeValue.
    * For text nodes, it's the text.
    */
    var $iNodeValue = "";
 
    /**
    * Field iNodeAttributes.
    * A string-indexed array containing attribute values
    * of the current node. Indexes are always lowercase.
    */
    var $iNodeAttributes;
 
    /**
    * Field iNodeStart.
    * The position of the first char.
    */
    var $iNodeStart;
 
    /**
    * Field iNodeEnd.
    * The position of the last char.
    */
    var $iNodeEnd;
 
    // The following fields should be 
    // considered private:
 
    var $iHtmlText;
    var $iHtmlTextLength;
    var $iHtmlTextIndex = 0;
    var $iHtmlCurrentChar;
    var $BOE_ARRAY;
    var $B_ARRAY;
    var $BOS_ARRAY;
 
    var $no_comment = false;
 
    //Liste des balises autofermantes
    var $BalisesSimples = array('hr', 'br', 'input', 'meta', 'link', 'img', 
'area', 'param');
 
    /**
    * Constructor.
    * Constructs an HtmlParser instance with
    * the HTML text given.
    */
    function HtmlParser ($aHtmlText) {
        $this->iHtmlText = $aHtmlText;
        $this->iHtmlTextLength = strlen($aHtmlText);
        $this->iNodeAttributes = array();
        $this->setTextIndex (0);
 
        $this->BOE_ARRAY = array (" ", "\t", "\r", "\n", "=" );
        $this->B_ARRAY = array (" ", "\t", "\r", "\n" );
        $this->BOS_ARRAY = array (" ", "\t", "\r", "\n", "/" );
    }
 
    /**
    * Method parse.
    * Parses the next node. Returns false only if
    * the end of the HTML text has been reached.
 
    * Updates values of iNode* fields.
    */
    function parse() {
        $this->iNodeStart = $this->iHtmlTextIndex;
        $text = $this->skipToElement();
        if ($text != "") {
            $this->iNodeType = NODE_TYPE_TEXT;
            $this->iNodeName = "Text";
            $this->iNodeValue = $text;
            $this->iNodeEnd = $this->iHtmlTextIndex;
            return true;
        }
        return $this->readTag();
    }
 
    function clearAttributes() {
        $this->iNodeAttributes = array();
    }
 
    function readTag() {
        if ($this->iCurrentChar != "<") {
            $this->iNodeType = NODE_TYPE_DONE;
            return false;
        }
        $this->clearAttributes();
        $this->skipMaxInTag ("<", 1);
        if ($this->iCurrentChar == '/') {
            $this->moveNext();
            $name = $this->skipToBlanksInTag();
            if (strtolower($name) == 'script') {
                $this->no_comment = false;
            }
            $this->iNodeType = NODE_TYPE_ENDELEMENT;
            $this->iNodeName = $name;
            $this->iNodeValue = "";
            $this->skipEndOfTag();
            $this->iNodeEnd = $this->iHtmlTextIndex;
            return true;
        }
        $name = $this->skipToBlanksOrSlashInTag();
        if (!$this->isValidTagIdentifier ($name)) {
            $comment = false;
            if ((strpos($name, "!--") === 0) && (!$this->no_comment)) {
                $ppos = strpos($name, "--", 3);
                if (strpos($name, "--", 3) === (strlen($name) - 2)) {
                    $this->iNodeType = NODE_TYPE_COMMENT;
                    $this->iNodeName = "Comment";
                    $this->iNodeValue = "<" . $name . ">";
                    $comment = true;
                } else {
                    $rest = $this->skipToStringInTag ("-->");
                    if ($rest != "") {
                        $this->iNodeType = NODE_TYPE_COMMENT;
                        $this->iNodeName = "Comment";
                        $this->iNodeValue = "<" . $name . $rest;
                        $comment = true;
                        // Already skipped end of tag
                        $this->iNodeEnd = $this->iHtmlTextIndex;
                        return true;
                    }
                }
            }
            if (!$comment) {
                $this->iNodeType = NODE_TYPE_TEXT;
                $this->iNodeName = "Text";
                $this->iNodeValue = "<" . $name;
                $this->iNodeEnd = $this->iHtmlTextIndex;
                return true;
            }
        } else {
            if (strtolower($name) == 'script') {
                $this->no_comment = true;
            }
            $this->iNodeType = NODE_TYPE_ELEMENT;
            $this->iNodeValue = "";
            $this->iNodeName = $name;
            while ($this->skipBlanksInTag()) {
                $attrName = $this->skipToBlanksOrEqualsInTag();
                if ($attrName != "" && $attrName != "/") {
                    $this->skipBlanksInTag();
                    if ($this->iCurrentChar == "=") {
                        $this->skipEqualsInTag();
                        $this->skipBlanksInTag();
                        $value = $this->readValueInTag();
                        $this->iNodeAttributes[strtolower($attrName)] = $value;
                    } else {
                        $this->iNodeAttributes[strtolower($attrName)] = "";
                        $this->setTextIndex ($this->iHtmlTextIndex - 1);
                    }
                }
            }
        }
        if (($this->iHtmlText{$this->iHtmlTextIndex - 1} == '/') || (in_array(
$this->iNodeName, $this->BalisesSimples))) {
            $this->iNodeType = NODE_TYPE_ELEMENT_END;
        }
        $this->skipEndOfTag();
        $this->iNodeEnd = $this->iHtmlTextIndex;
        return true;
    }
 
    function isValidTagIdentifier ($name) {
        return ereg ("^[A-Za-z0-9_\\-]+$", $name);
    }
 
    function skipBlanksInTag() {
        return "" != ($this->skipInTag ($this->B_ARRAY));
    }
 
    function skipToBlanksOrEqualsInTag() {
    return $this->skipToInTag ($this->BOE_ARRAY);
    }
 
    function skipToBlanksInTag() {
        return $this->skipToInTag ($this->B_ARRAY);
    }
 
    function skipToBlanksOrSlashInTag() {
        return $this->skipToInTag ($this->BOS_ARRAY);
    }
 
    function skipEqualsInTag() {
        return $this->skipMaxInTag ("=", 1);
    }
 
    function readValueInTag() {
        $ch = $this->iCurrentChar;
        $value = "";
        if ($ch == "\"") {
            $this->skipMaxInTag ("\"", 1);
            $value = $this->skipToInTag ("\"");
            $this->skipMaxInTag ("\"", 1);
        } elseif ($ch == "'") {
            $this->skipMaxInTag ("'", 1);
            $value = $this->skipToInTag ("'");
            $this->skipMaxInTag ("'", 1);
        } else {
            $value = $this->skipToBlanksInTag();
        }
        return $value;
    }
 
    function setTextIndex ($index) {
        $this->iHtmlTextIndex = $index;
        if ($index >= $this->iHtmlTextLength) {
            $this->iCurrentChar = -1;
        } else {
            $this->iCurrentChar = $this->iHtmlText{$index};
        }
    }
 
    function moveNext() {
        if ($this->iHtmlTextIndex < $this->iHtmlTextLength) {
            $this->setTextIndex ($this->iHtmlTextIndex + 1);
            return true;
        } else {
            return false;
        }
    }
 
    function skipEndOfTag() {
        while (($ch = $this->iCurrentChar) !== -1) {
            if ($ch == ">") {
            $this->moveNext();
            return;
            }
            $this->moveNext();
        }
    }
 
    function skipInTag ($chars) {
        $sb = "";
        while (($ch = $this->iCurrentChar) !== -1) {
            if ($ch == ">") {
                return $sb;
            } else {
                $match = false;
                for ($idx = 0; $idx < count($chars); $idx++) {
                    if ($ch == $chars[$idx]) {
                    $match = true;
                    break;
                    }
                }
                if (!$match) {
                    return $sb;
                }
                $sb .= $ch;
                $this->moveNext();
            }
        }
        return $sb;
    }
 
    function skipMaxInTag ($chars, $maxChars) {
        $sb = "";
        $count = 0;
        while (($ch = $this->iCurrentChar) !== -1 && $count++ < $maxChars) {
            if ($ch == ">") {
                return $sb;
            } else {
                $match = false;
                for ($idx = 0; $idx < count($chars); $idx++) {
                    if ($ch == $chars[$idx]) {
                    $match = true;
                    break;
                    }
                }
                if (!$match) {
                    return $sb;
                }
                $sb .= $ch;
                $this->moveNext();
            }
        }
        return $sb;
    }
 
    function skipToInTag ($chars) {
        $sb = "";
        while (($ch = $this->iCurrentChar) !== -1) {
            $match = $ch == ">";
            if (!$match) {
                for ($idx = 0; $idx < count($chars); $idx++) {
                    if ($ch == $chars[$idx]) {
                        $match = true;
                        break;
                    }
                }
            }
            if ($match) {
                return $sb;
            }
            $sb .= $ch;
            $this->moveNext();
        }
        return $sb;
    }
 
    function skipToElement() {
        $sb = "";
        while (($ch = $this->iCurrentChar) !== -1) {
            if ($ch == "<") {
                return $sb;
            }
            $sb .= $ch;
            $this->moveNext();
        }
        return $sb;
    }
 
    /**
    * Returns text between current position and $needle,
    * inclusive, or "" if not found. The current index is moved to a point
    * after the location of $needle, or not moved at all
    * if nothing is found.
    */
    function skipToStringInTag ($needle) {
        $pos = strpos ($this->iHtmlText, $needle, $this->iHtmlTextIndex);
        if ($pos === false) {
            return "";
        }
        $top = $pos + strlen($needle);
        $retvalue = substr ($this->iHtmlText, $this->iHtmlTextIndex, $top - 
$this->iHtmlTextIndex);
        $this->setTextIndex ($top);
        return $retvalue;
    }
}
 
function HtmlParser_ForFile ($fileName) { 
    return HtmlParser_ForURL($fileName);
}
 
function HtmlParser_ForURL ($url) {
    $fp = fopen ($url, "r");
    $content = "";
    while (true) {
        $data = fread ($fp, 8192);
        if (strlen($data) == 0) {
            break;
        }
        $content .= $data;
    }
    fclose ($fp);
    return new HtmlParser ($content);
}
 
function TronqueHtml($chaine, $max, $separateur = ' ', $suffix = ' ...') {
    if (strlen(strip_tags($chaine)) > $max) {
        $tabElements = array();
        $cur_len = 0;
        $parser = new HtmlParser($chaine);
        while ($parser->parse()) {
            if ($parser->iNodeType == NODE_TYPE_ELEMENT) {
                array_push($tabElements, $parser->iNodeName);
            } elseif ($parser->iNodeType == NODE_TYPE_ENDELEMENT) {
                while (array_pop($tabElements) != $parser->iNodeName) {
                    if (count($tabElements) < 1) {
                        echo 'Erreur : pas de balise ouvrante pour ' . $parser->
iNodeName;
                    }
                }
            } elseif ($parser->iNodeType == NODE_TYPE_TEXT) {
                $cur_max = $cur_len + $parser->iNodeEnd - $parser->iNodeStart;
                if ($cur_max == $max) {
                    $resultat = substr($chaine, 0, $parser->iNodeEnd) . $suffix;
                    while (($balise = array_pop($tabElements)) !== null) {
                        $resultat .= '</' . $balise . '>';
                    }
                    return $resultat;
                } elseif ($cur_max > $max) {
                    if (($pos = strrpos(substr($parser->iNodeValue, 0, ($max - 
$cur_len + strlen( $separateur ))), $separateur)) !== false) {
                        $resultat = substr($chaine, 0, $parser->iNodeStart + 
$pos) . $suffix;
                        while (($balise = array_pop($tabElements)) !== null) {
                            $resultat .= '</' . $balise . '>';
                        }
                        return $resultat;
                    } else {
                        $resultat = substr($chaine, 0, $parser->iNodeEnd) . 
$suffix;
                        while (($balise = array_pop($tabElements)) !== null) {
                            $resultat .= '</' . $balise . '>';
                        }
                        return $resultat;
                    }
                } else {
                    $cur_len += $parser->iNodeEnd - $parser->iNodeStart;
                }
            }
        }
    }
    return $chaine;
}
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test tronque html</title>
</head>
<body>
<?php
$chaine = '<h2>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. </h2>
<p>
<span class="Style1">Praesent tortor purus, <strong>commodo</strong> quis,' .
' interdum et, tincidunt ut, lacus.
Etiam condimentum volutpat dolor. Proin faucibus libero eu lectus.<br />
<em>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <strong>Nam' .
' aliquam tellus eget ligula</strong>. Phasellus pretium neque ut felis.' .
' Quisque lacinia congue ante. Lorem ipsum dolor sit amet, consectetuer' .
' adipiscing elit. Quisque at metus quis tortor auctor faucibus.</em></span><b' .
'r />
Curabitur quis lectus. Integer felis est, congue id, luctus quis, congue' .
' volutpat, lacus. Curabitur malesuada felis semper nisl. Aenean laoreet. Nunc' .
' vitae nisi. <br />
</p>';
echo "Tronqué à 280 caractères (hors balises) :<br>\n";
echo TronqueHtml($chaine, 280, ' ', ' ...');
echo "\n<hr>\n";
echo $chaine;
?>
</body>
</html>
betadev est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/04/2011, 08h19   #4
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Merci, Je vais regader de plus près la fonction que tu me proposes pour voir si elle correspond à mes besoins.

La fonction que je proposais me posais pour l'instant aucun souci mise à part qu'elle coupait les mots entre deux pages...

je te tient au courant de l'adaptation.
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/04/2011, 10h43   #5
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Bonjour,

J'ai essayé d'adapter la fonction à mon besoin mais étrangement, il y a des erreurs de découpage

De plus, la fonction semble également couper les mots

Voici le code qui appel la fonction :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//calcul nb_page
			$nb_page = 0;
			$pages = array();
			//appel de couper_texte
 
 
			$nombreDeCaractParPage = 50;
			$reste_contenu = $contenu;
 
			$indice_coupe = 50;
			$cumul_coupe = 0;
 
			while ($indice_coupe >= $nombreDeCaractParPage )
			{
 
				$cumul_coupe += $indice_coupe;
				$indice_coupe = TronqueHtml($reste_contenu, $nombreDeCaractParPage, ' ', ' ...');
				$indice_coupe = strlen($indice_coupe);
 
 
				$pages[$nb_page]['texte'] = substr($reste_contenu,0,$indice_coupe);
				//page découpée
				$pages_texte = $pages[$nb_page]['texte'];
				echo $pages_texte;
				echo'----------------------------<br />';
 
				$reste_contenu = substr($reste_contenu,$indice_coupe);
			}
les erreurs portent sur les balises. La fonction me dit qu'il manque des balises alors que je prends le code de démo...

Est-ce que quelqu'un peut m'aider ?

Merci
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 10h26.


 
 
 
 
Partenaires

Hébergement Web