IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Mélanger PHP HTML et BBcode


Sujet :

Langage PHP

  1. #1
    Candidat au Club
    Inscrit en
    Juin 2009
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2009
    Messages : 6
    Points : 2
    Points
    2
    Par défaut Mélanger PHP HTML et BBcode
    Bonjour le forum !
    J'ai besoin d'aide. Je m'y connais pas trop en html, mais je vais vous expliquer mon problème :
    J'ai un fichier php qui permet d'écrire dans un fichier .txt, et de lire ce qu'il y a dans le fichier .txt
    Tout marche bien. Maintenant j'aimerai l'améliorer en saisissant du code "bbcode", et lorsque j'envoie, j'obtiens du code html dans le fichier .txt (en gros ça traduit)

    J'ai trouvé plusieurs tutos sur internet, j'ai mes codes à vous presenter
    D'abord mon fichier php qui permet d'ecrire dans un fichier txt et qui se nome "edittext.php":
    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
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
    <head> 
    <title>Text Editor</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <style>
    <!--
    td{font-family:verdana,sans-serif; font-size:8pt;color:#333333};
    body{font-family:verdana,sans-serif; font-size:11pt;color:#333333;font-weight:bold};
    //-->
    </style>
     
    </head> 
     
    <body bgcolor="#eeeedd"> 
    <center>
    Infos du jour<br><br>
    <table border="0" cellpadding="10" cellspacing="0" style="border:2px solid #ddddcc"><tr><td align="middle" bgcolor="#eeeedd">
    <form action="edittext.php" method="post"> 
    <table width="300" border="0"> 
    <tr> 
    <td><textarea name="text_file" rows="20" cols="40"></textarea></td> 
    </tr> 
    </table> 
    <input type="submit" value="Get" name="gettext">  <input type="submit" value="Submit" name="submit">  <input type="reset" value="Reset"> 
    </form>
    text sur my data<br><br></td></tr><tr><td align="left" width="500" bgcolor="#eeeedd"><hr color=#ddddcc> 
    <?php 
    // var_dump($_POST);
     
     
    if (isset($_POST[gettext])){
     
    	$myfile = fopen("my_data.txt","r");
    	$mydata = fread($myfile,filesize("my_data.txt")); 
    	print $mydata; 
    }
    else if (isset($_POST[submit])){ 
    	$myfile = fopen ("my_data.txt","w+"); 
    	$entry = $_POST[text_file];
    	$entry = ereg_replace("\n"," ",$entry); 
    	$entry = stripslashes($entry); 
    	$mydata = "&mavariable= $entry \n\n"; 
    	fwrite($myfile,$mydata); 
    	fclose($myfile); 
    	$myfile = fopen("my_data.txt","r"); 
    	$mydata = fread($myfile,filesize("my_data.txt")); 
    	print $mydata; 
    } 
     
    ?> 
    <hr color=#ddddcc> 
    </td></tr></table>
    </center>
    </body> 
    </html>
    Ensuite on fichier texte qui se nomme : "my_data.txt" ,et dans ce fichier il est écrit : &mavariable=

    Puis voilà ce que j'ai trouvé pour le bbcode dont le fichier se nomme "test.php:

    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
    <?php
    require_once ('inc_cg_filter.php');
    $filter = new cg_filter;
     
    $filter->html_filter_prefs['url_parsing'] = 0; // On désactive la détection automatique des liens
     
    // Enlevez les espaces dans les balises !
    $filter->string = "[b ]En gras ![/b]\n[i ]En italique ![/i]\n[u ]En surligné ![/u]\n[b ][i ][u ]En gras, italique et surligné ![/u][/i][/b]\n\n"; // il faut enlever les espaces au niveau des balises
     
    $filter->html_filter(); // Filtrage HTML
    $filter->bbcode_to_html(); // Conversion BBCode en HTML;
     
    echo ($filter->string); // Affichage HTML
     
    $filter->html_to_bbcode(); // Conversion HTML en BBCode
     
    echo ($filter->string); // Affichage BBCode
    ?>
    Et ce fichier ci-dessus fait appel à celui-là pour des fonctions dont voici le code et qui se nomme "inc_cg_filter.php":


    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
    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
    <?php
    // ***************************************************************
    // Date: 08/08/2005 - File name: inc_cg_filter.php
    // Version: 1.02
    //
    // This is the Coolguys HTML filter class.
    //
    // It contains BBCode to HTML and HTML to BBCode converters,
    // HTML filtering functions, URL and smiley parsing.
    // ***************************************************************
     
    class cg_filter {
     
    	var $string;
    	var $smiley_path = 'images/smileys/'; // Do not forget trailing '/'
    	var $html_filter_prefs = array('entities' => 1, 'nl2br' => 1, 'url_parsing' => 1, 'smileys' => 1); // By default, apply all filtering
     
    	function bbcode_to_html() {
     
    		// ***************************************************************
    		// This function parses BBCode into HTML.
    		// ***************************************************************
     
    		$pos = strpos($this->string, '[');
     
    		while ($pos !== FALSE) {
    			$len = strpos($this->string, ']', $pos + 1) - $pos + 1;
     
    			if ($len) { // No need to continue is there is no ]
     
    				$rest = explode('=', substr($this->string, $pos + 1, $len - 2), 2);
    				$tag = $rest[0];
     
    				if (($pos2 = strpos($this->string, "[/$tag]", $pos + $len)) === FALSE) { // No end tag
    					$tag = NULL;
    				} else {
    					$param_name = substr($this->string, $pos + $len, $pos2 - $pos - $len);
    					$len2 = strpos($this->string, ']', $pos2 + 1) - $pos2 + 1;
    				}
     
    				switch($tag) {
    				case 'i':
    				case 'u':
    				case 'b':
    				case 's':
     
    					$this->string = substr_replace($this->string, "<$tag>", $pos, $len);
    					$this->string = substr_replace($this->string, "</$tag>", $pos2, $len + 1);
    					break;
     
    				case 'img':
     
    					$replace_str = '<img src="' . $param_name . '" alt="' . basename($param_name) . '" />';
     
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'url':
     
    					if (!$rest[1])
    						$rest[1] = $param_name;
     
    					$target = NULL;
    					if (substr($rest[1], 0, 7) == 'http://')
    						$target = ' target="_blank"';
     
    					if ($param_name)
    						$replace_str = '<a href="' . $rest[1] . '"' . $target . '>' . $param_name . '</a>';
    					else
    						$replace_str = '<a href="' . $rest[1] . '"' . $target . '>' . $rest[1] . '</a>';
     
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'align':
     
    					$replace_str = '<div align="' . $rest[1] . '">' . $param_name . '</div>';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'list':
     
    					$pos_del = strpos($param_name, '[*]');
    					$param_name = substr_replace($param_name, '', 0, $pos_del + 3); // Ignore first[*]
    					$replace_str = '<ul><li>';
    					$replace_str .= str_replace('[*]', '</li><li>', $param_name);
    					$replace_str .= '</li></ul>';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'code':
    				case 'quote':
     
    					$replace_str = '<div class="' . $tag . '">' . $param_name . '</div>';
     
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'anchor':
     
    					$replace_str = '<a name="' . $param_name . '"></a>';
     
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				default:
    					// Prevents infinite loops should the string contain "[" char without it being a BBCode
    					$pos++;
    					break;
    				}
    			}
     
    			// Repeat
    			$pos = strpos($this->string, '[', $pos);
    		}
    	}
     
    	function html_to_bbcode() {
     
    		// ***************************************************************
    		// This function parses HTML into BBCode.
    		// ***************************************************************
     
    		$open_tags = array ();
     
    		$pos = strpos($this->string, '<');
     
    		while ($pos !== FALSE) {
    			$len = strpos($this->string, '>', $pos + 1) - $pos + 1;
     
    			if ($len) { // No need to continue is there is no >
    				$rest = explode('=', substr($this->string, $pos + 1, $len - 2), 2);
    				$tag = explode(' ', $rest[0]);
    				$tag = $tag[0];
     
    				if (($pos2 = strpos($this->string, "</$tag>", $pos + $len)) === FALSE) { // No end tag
    					// Let's see if it's a tag without end tag, i.e. <br />, or </div>
    					$ok_tags = array ('</div>');
    					if (!in_array(substr($this->string, $pos, $len), $ok_tags) && strpos($this->string, '/>', $pos + 1) === FALSE)
    						$rest[0] = NULL;
    				} else {
    					$param_name = substr($this->string, $pos + $len, $pos2 - $pos - $len);
    					$len2 = strpos($this->string, '>', $pos2 + 1) - $pos2 + 1;
    				}
     
    				switch($rest[0]) {
    				case 'i':
    				case 'u':
    				case 'b':
    				case 's':
     
    					$this->string = substr_replace($this->string, "[$tag]", $pos, $len);
    					$this->string = substr_replace($this->string, "[/$tag]", $pos2, $len + 1);
    					break;
     
    				case 'a href':
     
    					$link = explode('"', $rest[1]);
    					$replace_str = '<a href="http://. $link[1] ." target="_blank">' . $param_name . '</a>';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'a name':
     
    					$link = explode('"', $rest[1]);
    					$replace_str = '[anchor]' . $link[1] . '[/anchor]';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'div align':
     
    					$link = explode('"', $rest[1]);
    					array_push($open_tags, 'align');
    					$replace_str = '[align=' . $link[1] . ']' . $param_name;
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 - $pos);
    					break;
     
    				case 'div class':
     
    					$link = explode('"', $rest[1]);
    					array_push($open_tags, $link[1]);
    					$replace_str = '[' . $link[1] . ']' . $param_name;
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 - $pos);
    					break;
     
    				case '/div':
     
    					if (count($open_tags) > 0)
    						$replace_str = '[/' . array_pop($open_tags) . ']';
    					else
    						$replace_str = '';
    					$this->string = substr_replace($this->string, $replace_str, $pos, 6);
    					break;
     
    				case 'img src':
     
    					$link = explode('"', $rest[1]);
    					$replace_str = '[img]' . $link[1] . '[/img]';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $len);
    					break;
     
    				case 'br /':
     
    					$replace_str = '';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $len);
    					break;
     
    				case 'ul':
     
    					$replace_str = '<ul><li style="">' . $param_name . '</li></ul>';
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				case 'li':
     
    					$replace_str = '[*]' . $param_name;
    					$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
    					break;
     
    				default:
    					// Prevents infinite loops should the string contain "<" char without it being html
    					$pos++;
    					break;
    				}
    			}
     
    			// Repeat
    			$pos = strpos($this->string, '<', $pos);
    		}
     
    		// Close any open tags, in case the html contains errors
    		if (count($open_tags) > 0) {
    			foreach($open_tags as $value) {
    				$this->string .= '[/' . array_pop($open_tags) . ']';
    			}
    		}
     
    	}
     
    	function html_filter() {
     
    		// ***************************************************************
    		// This function provides basic html filtering.
    		// By default, all rules are applied; should you require to
    		// disable some rules, the html_filter_prefs array can be used.
    		//
    		// Ex: $my_class->html_filter_prefs['smileys'] = 0; // No smileys
    		// ***************************************************************
     
    		// First we make sure there is not html
    		if ($this->html_filter_prefs['entities'])
    			$this->string = htmlspecialchars($this->string, ENT_NOQUOTES);
     
    		// Smileys
    		if ($this->html_filter_prefs['smileys']) {
    			$smileys = array (':)' => 0, ':-)' => 0, ';)' => 1, ';-)' => 1, ':D' => 2, ':-D' => 2, ':P' => 3, ':-P' => 3, ':p' => 3, ':-p' => 3, ':(' => 4, ':-(' => 4);
    			$filename = array('happy.gif', 'wink.gif', 'biggrin.gif', 'tongue.gif', 'sad.gif');
     
    			foreach ($smileys as $key => $value) {
    				$img_url = '<img src="' . $this->smiley_path . $filename[$value] . '" alt="' . $key . '" />';
    				$this->string = str_replace($key, $img_url, $this->string);
    			}
    		}
     
    		// This automatically converts new lines and tabs
    		if ($this->html_filter_prefs['nl2br']) {
    			$this->string = nl2br($this->string);
    			$this->string = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $this->string);
    		}
     
    		// Automatic URL parsing
    		if ($this->html_filter_prefs['url_parsing']) {
    			// For 'http://example.com/?id=' type links
    			$this->string = ereg_replace('[ ]+([a-z]+://[-a-z0-9A-Z&amp;_\/\.\?\=]+)[ ]+', ' <a href="\\1" target="_blank">\\1</a> ', $this->string);
    			// For 'www.example.com' type links
    			$this->string = ereg_replace('[ ]+(www\.[-a-z0-9A-Z\.]+\.[-a-z0-9A-Z&amp;_\/\.\?\=]+)[ ]+', ' <a href="http://\\1" target="_blank">http://\\1</a> ', $this->string);
    		}
    	}
     
    }
    ?>

    Donc ce que je veux, c'est mélanger mon fichier edittext.php et test.php afin de saisir du code bbcode dans l'éditeur, et lorsque je fais "envoi" ,ça traduit ce code que j'ai saisis et que je retrouve la traduction en html dans mon fichier texte .

    Oui le post est un peu long

    Merci d'avance à ceux qui m'aideront.

  2. #2
    Membre chevronné Avatar de nosferapti
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    1 157
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 1 157
    Points : 1 895
    Points
    1 895
    Par défaut
    qu'est ce qui ne fonctionne pas avec le code que tu nous a montré ?
    GNAP !

  3. #3
    Candidat au Club
    Inscrit en
    Juin 2009
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2009
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    bonjour nosferapti,

    Je n'ai jamais dit que ça fonctionnais pas.
    je voulais juste ameliorer ma saisie de texte en php en inserant du bbcode et lorsque je valide la saisie, je récupère le tout en code html dans un fichier .txt

    Actuellement je ne saisie que du texte normal, c'est tout.
    et c'est pour ça que j'ai trouvé un code concernant sur le bbcode, et que j'ai posté, et je voudrai bien assembler ce "bbcode" avec mon edittext.

  4. #4
    Expert confirmé
    Avatar de Thes32
    Homme Profil pro
    Développeur PHP, .Net, T-SQL
    Inscrit en
    Décembre 2006
    Messages
    2 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Développeur PHP, .Net, T-SQL

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 379
    Points : 4 853
    Points
    4 853
    Par défaut
    Salut,

    voilà ce qui peut beaucoup t'aider
    http://php.developpez.com/faq/?page=...es_parsebbcode

    A +
    Développeur | Zend Certified Engineer

    Étapes Pour mieux se servir du forum:
    1. Commencez par lire les cours et tutoriels ;
    2. Faites une recherche;
    3. Faites un post si rien trouvé dans les deux étapes précédentes en respectant les règles;

    Nix>_Rien n'est plus pratique que la théorie

  5. #5
    Candidat au Club
    Inscrit en
    Juin 2009
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2009
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    Merci beaucoup je vai voir ça

Discussions similaires

  1. convertire du html en bbcode à l'aide de php
    Par young077 dans le forum Langage
    Réponses: 4
    Dernier message: 23/03/2011, 11h01
  2. [PHP/HTML] Soumission d'un fromulaire par "entrée"
    Par dychentrus dans le forum Langage
    Réponses: 5
    Dernier message: 28/11/2005, 18h14
  3. quel language complete PHP HTML ou xHTML
    Par ghita269 dans le forum Langage
    Réponses: 3
    Dernier message: 21/10/2005, 19h39
  4. [Couleur] Différence entre Delphi, Php/Html
    Par Rayek dans le forum Langage
    Réponses: 5
    Dernier message: 19/10/2005, 13h06
  5. [FLASH MX] php => html => Flash MX
    Par Jefekoi dans le forum Flash
    Réponses: 10
    Dernier message: 03/09/2005, 21h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo