Bonjour à tous,
J'ai problème de comment faire une boucle avec condition pour rendre un résumer d'un texte.
synthèse:
texte:Donc le résumer afficher comme ça:Paragraphe 1: mot1 mot2 mot3 ... mot n. // phrase termine avec un parmi ces séparateur( .?!,et pragraphe termine avec un retour à la ligne.
Paragraphe 2: mot1 mot2 mot3 ... mot n. // phrase termine avec un parmi ces séparateur( .?!,et pragraphe termine avec un retour à la ligne.
...
...
...
Paragraphe N: mot1 mot2 mot3 ... mot n. // phrase termine avec un parmi ces séparateur( .?!,et pragraphe termine avec un retour à la ligne.
Si on veux ajouter un sélectionneur de pourcentage pour résumer un texte:
Afficher une phrase a partir du paragraphe 1
jusqu'à la premier phrase du paragraphe N.
on va mettre par exemple si on veux 50% il faut tronquer la phrase 1 et passer à la phrase 3, phrase 5 ansi de suite jusqu'à la phrase (N-1) par exemple.
et le résumer afficher comme ça: Phrase 1 + phrase 3 + phrase (N-1) .
Voici mon code source:
La fonction tronquer:
la page traitement et affichage:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <?php function tronquer($description) { //nombre de caractères à afficher $max_caracteres=30; // Test si la longueur du texte dépasse la limite if (strlen($description)>$max_caracteres) { // Séléction du maximum de caractères $description = substr($description, 0, $max_caracteres); // Récupération de la position du dernier espace (afin déviter de tronquer un mot) $position_espace = strrpos($description, " "); $description = substr($description, 0, $position_espace); // Ajout des "..." $description = $description."..."; } return $description; } ?>
Comment ça marche pour attendre un résumer
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 // form qui va permet au utilisateur de saisir un texte <form action="" method="post"> <td width="16%"> </td> <td width="66%" align="center"> <strong>Saisissez/collez le texte à résumer dans le champs si dessous puis </strong> <input type="submit" name="validez" value="Validez"> <br> <textarea name="resumer" rows="15" cols="110"><?php if (isset($_POST['resumer'])) echo $_POST['resumer'] ; ?> </textarea> <br> <input name="ok" type="submit" value="Valider"> </td> <td width="18%" align="center"><table width="100%" border="0"> <tbody><tr> <td align="center"><font size="1"><a href="www.3wmedia.ma">-aide-</a></font></td> </tr> <tr> <td> </td> </tr> </tbody></table> <table width="100%" border="0"> <tbody><tr> <td align="center" bgcolor="#000000"><strong><font color="#FFFFFF">Options</font></strong></td> </tr> <tr> <td><select name="langue"> <option value="en">Arabe</option> <option value="fr">Français</option> <option value="en">English</option> </select> Langue </td> </tr> <tr> <td><select name="compress"> <option value="30">30%</option> <option value="10">10%</option> <option value="20">20%</option> <option value="30">30%</option> <option value="40">40%</option> <option value="50">50%</option> <option value="60">60%</option> <option value="70">70%</option> </select> Compression</td> </tr> <tr> <td> <input name="html" type="checkbox" value="checked"> afficher la source</td> </tr> </tbody></table></td> </form> // fin form... // traitement PHP <?php $chaine=""; $max_sujet="30"; if (isset($_POST['resumer'])){ $chaine=$_POST['resumer']; $sujet=$chaine; //traitement sur le titre qui doit affichier au sujet... if (strlen($sujet)>$max_sujet) { // Séléction du maximum de caractères $sujet = substr($sujet, 0, $max_sujet); // Récupération de la position du dernier espace (afin déviter de tronquer un mot) $position_espace = strrpos($sujet, " "); $sujet = substr($sujet, 0, $position_espace); // Ajout des "..." $sujet = $sujet."..."; } echo "<b>$sujet</b><br/>"; include"function_tronquer.php";//include de la fonction tranquer echo tronquer($chaine);// ici je veux le résumer j'ai pas compris comment boucler et afficher les phrases l'une après l'autre avec le calcule du pourcentage. } ?> // Fin traitement PHP!!
Partager