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 :

regex par block


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut regex par block
    Bonjour, j'aimerais que ce bout de code est effet sur plusieur ligne comment puis-je faire ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $texte = preg_replace('#(\[HTML\].*)class="(.*?)"(.*\[/HTML\])#', '$1 <SPAN CLASS="balisehtmltype">class</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);

  2. #2
    Expert confirmé
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Par défaut
    Hello

    Utilise la balise [code].

    Utilise le modificateur pcre 'm' (pour multiline): http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

  3. #3
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut
    Je sais que j'en demande beaucoup mais pourais tu me faire un exemple avec la ligne que j'ai mi dans l'enoncer stp

  4. #4
    Expert confirmé
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    $texte = preg_replace('#(\[HTML\].*)class="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">class</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);

  5. #5
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut
    Cela ne marche pas :s

  6. #6
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut
    Voici un peux plus de précision sur mon code

    $texte = '[html]
    <body>
    </body>
    [/html]';

    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
     
    // Je remplace les balises bb comprises entre des balises HTML
    $texte = preg_replace('#(\[HTML\].*)<h1>(.*?)</h1>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;h1&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/h1&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<h2>(.*?)</h2>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;h2&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/h2&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<h3>(.*?)</h3>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;h3&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/h3&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<h4>(.*?)</h4>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;h4&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/h4&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<h5>(.*?)</h5>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;h5&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/h5&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<h6>(.*?)</h6>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;h6&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/h6&gt;</SPAN>$3', $texte);
     
    $texte = preg_replace('#(\[HTML\].*)(.*?)<br>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;br&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)(.*?)<br />(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;br /&gt;</SPAN>$3', $texte);
     
    $texte = preg_replace('#(\[HTML\].*)<p>(.*?)</p>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;p&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/p&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<em>(.*?)</em>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;em&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/em&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<strong>(.*?)</strong>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;strong&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/strong&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<html>(.*?)</html>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;html&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/html&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<title>(.*?)</title>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;title&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/title&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<head>(.*?)</head>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;head&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/head&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<body>(.*?)</body>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;body&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/body&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<q>(.*?)</q>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;q&gt;</SPAN><SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/q&gt;</SPAN>$3', $texte);
     
    $texte = preg_replace('#(\[HTML\].*)<a(.*?)>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;a</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<acronym(.*?)>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;acronym</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<html(.*?)>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;html</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<body(.*?)>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;body</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)>(.*?)</a>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/a&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)>(.*?)</acronym>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtmltexte">$2</SPAN><SPAN CLASS="balisehtml">&lt;/acronym&gt;</SPAN>$3', $texte);
     
    $texte = preg_replace('#(\[HTML\].*)<img(.*?)/>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;img</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">/&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<img(.*?)>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;img</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">&gt;</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)<span(.*?)>(.*\[/HTML\])#m', '$1<SPAN CLASS="balisehtml">&lt;span</SPAN>&nbsp;$2<SPAN CLASS="balisehtml">&gt;</SPAN>$3', $texte);
     
    $texte = preg_replace('#(\[HTML\].*)class="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">class</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)http-equiv="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">http-equiv</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)xmlns="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">xmlns</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)content="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">content</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)rel="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">rel</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)media="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">media</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)type="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">type</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)href="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">href</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)title="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">title</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)alt="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">alt</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)src="(.*?)"(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">src</SPAN>=<SPAN CLASS="balisehtmlname">"$2"</SPAN>$3', $texte);
    $texte = preg_replace('#(\[HTML\].*)target=(.*?)&nbsp(.*\[/HTML\])#m', '$1 <SPAN CLASS="balisehtmltype">target</SPAN>=<SPAN CLASS="balisehtmlnameautre">$2</SPAN>$3', $texte);
     
    // je supprime les balises HTML
    $texte = preg_replace('#\[HTML\](.*)\[/HTML\]#m','<br><span class="codetype">CODE HTML :</span><div class="code">$1</div>', $texte);
     
    echo $texte;
    ?>

  7. #7
    Expert confirmé
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Par défaut
    alors essaie le 'sm' ou change de pattern. Mets aussi les caractères '^' et '$' pour marquer le début ou la fin de chaine dans le pattern ou l'option 'm' n'aura aucun effet.

  8. #8
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut balise fin debut
    Salutation,

    voila je me creuse la tête depuis deux jours sans trouver une solution a mon problème
    Peut être pourriez vous m'aider ... c'est ce que l'on va voir

    Alors voila j'aimerais faire en sorte que a chaque fois que j'envoie un texte par la méthode post dans mon texte se rajoute a chaque début de ligne la balise [html] et a chaque fin de ligne [/html] voici un exemple pour vous éclairer :

    mon texte initial :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
    <head>
    <title>Exemple d\'utilisation de CSS externe</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
    <link rel="stylesheet" media="screen" type="text/css" title="Essai" href="essai.css" >
    </head>
    <body>
    <p>
    </p>
    <p>
    </p>
    </body>
    </html>
    et apres l'envoi cela donne ça :
    [html]<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >[/html]
    [html]<head>[/html]
    [html]<title>Exemple d\'utilisation de CSS externe</title>[/html]
    [html]<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >[/html]
    [html]<link rel="stylesheet" media="screen" type="text/css" title="Essai" href="essai.css" >[/html]
    [html]</head>[/html]
    [html]<body>[/html]
    [html]<p>[/html]
    [html]</p>[/html]
    [html]<p>[/html]
    [html]</p>[/html]
    [html]</body>[/html]
    [html]</html>[/html]
    comment puis-je faire ?

  9. #9
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Par défaut
    Tu es arrivé a quoi pendant ces deux jours de reflexion ?
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  10. #10
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut
    Citation Envoyé par sabotage Voir le message
    Tu es arrivé a quoi pendant ces deux jours de reflexion ?
    Rien malheureusement

  11. #11
    Expert confirmé

    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2010
    Messages
    5 420
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

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

    Informations forums :
    Inscription : Septembre 2010
    Messages : 5 420
    Par défaut
    Peut être pourrais-tu envisager de faire un explode sur les retours lignes... pour commencer.

  12. #12
    Membre très actif
    Avatar de sourcilpower
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    128
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2011
    Messages : 128
    Billets dans le blog
    2
    Par défaut
    Pourais tu m'expliquer d'avantage ? car je debute moi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Regex : remplacer une regex par une variable
    Par Julipp dans le forum Général Python
    Réponses: 5
    Dernier message: 07/04/2013, 15h03
  2. Décrypter RIJNDAEL_128 par block
    Par cid007300 dans le forum Langage
    Réponses: 1
    Dernier message: 20/02/2013, 17h43
  3. [RegEx] supprimer <a href...</a> par regex dans pages html.
    Par moldak dans le forum Langage
    Réponses: 10
    Dernier message: 15/11/2012, 16h02
  4. [SP-2010] Affichage des colonnes par block
    Par bdptaki dans le forum SharePoint
    Réponses: 3
    Dernier message: 24/10/2011, 15h47
  5. [RegEx] regex par block
    Par sourcilpower dans le forum Langage
    Réponses: 14
    Dernier message: 28/02/2011, 16h34

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