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 :

strip_tags, comportement etonnant


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    495
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 495
    Par défaut strip_tags, comportement etonnant
    Bonjour,

    j'ai un probleme avec la fonction strip_tags.

    Je recois un texte d'un webservice.. Je fais quelques traitements dessus avant de l'afficher.. et ca ne marche pas comme ca devrait..

    un petit exemple est plus parlant.. :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <?php
    $value='&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Rendez-vous des participants &amp;agrave; l\'a&amp;eacute;roport.&lt;o:p>&lt;/o:p>&lt;/span>&lt;/p>&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Assistance aux formalit&amp;eacute;s d\'enregistrement et d\'embarquement, puis d&amp;eacute;collage &amp;agrave; destination de &lt;strong style="mso-bidi-font-weight: normal">Louxor. Collation servie &amp;agrave; bord.&lt;o:p>&lt;/o:p>&lt;/strong>&lt;/span>&lt;/p>&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Accueil &amp;agrave; l\'arriv&amp;eacute;e et assistance aux formalit&amp;eacute;s d\'entr&amp;eacute;e.&lt;o:p>&lt;/o:p>&lt;/span>&lt;/p>&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;strong style="mso-bidi-font-weight: normal">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Transfert et installation &amp;agrave; bord du bateau. Nuit &amp;agrave; bord.&lt;o:p>&lt;/o:p>&lt;/span>&lt;/strong>&lt;/p>';
     
    $value = (String)$value;
    $value = html_entity_decode($value);
    $value = tidy_repair_string($value);
    echo $value;
    echo strip_tags($value);
    bon, ca n'est pas le code qui pose probleme, mais il le reproduit, en simplifié..

    mon echo $value donne ca : (tout a fait normal)
    · Rendez-vous des participants à l'aéroport.

    · Assistance aux formalités d'enregistrement et d'embarquement, puis décollage à destination de Louxor. Collation servie à bord.

    · Accueil à l'arrivée et assistance aux formalités d'entrée.

    · Transfert et installation à bord du bateau. Nuit à bord.


    Mais le echo strip_tags($value); donne ca :
    · Rendez-vous des participants à l'aéroport.
    => j'ai perdu les 3/4 de mon texte...

    j'ai rajouté le tidy_clean_repair en esperant que le souci vienne d'un html mal formé, mais ca ne change rien..

    D'ou peut venir le fait que strip_tags me zappe les 3/4 du texte ???

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    495
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 495
    Par défaut
    bon bah .. j'ai changé mon strip tags par une fonction preg_replace.. ca marche mieux :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    function html2txt($document){
    	$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
    	'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    	'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    	'@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
    	);
    	$text = preg_replace($search, '', $document);
    	return $text;
    }
    sauf qu'on ne peut pas preciser les tags a garder

  3. #3
    Expert confirmé Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Par défaut
    à la suite de l'appel à tidy_repair_string() est-ce que tu peux appeller la fonction suivante:
    Et nous coller le résultat ici ?

    (Si tu passe par un navigateur alors colle plutot le resultat du var_dump à partir du code source de la page html)

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    495
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 495
    Par défaut
    bien sur ..
    donc voila le code appliqué :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    $value = (String)$value;
    $value = html_entity_decode($value);
    $value = tidy_repair_string($value);
    var_dump($value);
    et le resultat :
    string(2787) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <p class="MsoPlainText" style=
    "MARGIN: 2pt 0cm 2pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">
    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">
    <span style="mso-list: Ignore">&middot;<span style=
    "FONT: 7pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span>
    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Rendez-vous
    des participants &agrave; l'a&eacute;roport.</span></p>
    <p class="MsoPlainText" style=
    "MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">

    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">
    <span style="mso-list: Ignore">&middot;<span style=
    "FONT: 7pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span>
    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Assistance
    aux formalit&eacute;s d'enregistrement et d'embarquement, puis
    d&eacute;collage &agrave; destination de <strong style=
    "mso-bidi-font-weight: normal">Louxor. Collation servie &agrave;
    bord.</strong></span></p>
    <p class="MsoPlainText" style=
    "MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">
    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">
    <span style="mso-list: Ignore">&middot;<span style=
    "FONT: 7pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span>
    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Accueil
    &agrave; l'arriv&eacute;e et assistance aux formalit&eacute;s
    d'entr&eacute;e.</span></p>

    <p class="MsoPlainText" style=
    "MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">
    <span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">
    <span style="mso-list: Ignore">&middot;<span style=
    "FONT: 7pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span>
    <strong style="mso-bidi-font-weight: normal"><span style=
    "FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Transfert
    et installation &agrave; bord du bateau. Nuit &agrave;
    bord.</span></strong></p>
    </body>
    </html>"

    Ceci dit.. la fonction avec preg_replace marche bien.. donc je crois que je ne vais pas me casser plus la tete la dessus.. meme si c'est un peu plus gourmand en traitements..

  5. #5
    Expert confirmé Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Par défaut
    Ok merci...

    Je ne comprends pas trop ton problème, je n'arrive pas à le reproduire... Si je prend le code suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <?php
    $value='&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Rendez-vous des participants &amp;agrave; l\'a&amp;eacute;roport.&lt;o:p>&lt;/o:p>&lt;/span>&lt;/p>&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Assistance aux formalit&amp;eacute;s d\'enregistrement et d\'embarquement, puis d&amp;eacute;collage &amp;agrave; destination de &lt;strong style="mso-bidi-font-weight: normal">Louxor. Collation servie &amp;agrave; bord.&lt;o:p>&lt;/o:p>&lt;/strong>&lt;/span>&lt;/p>&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Accueil &amp;agrave; l\'arriv&amp;eacute;e et assistance aux formalit&amp;eacute;s d\'entr&amp;eacute;e.&lt;o:p>&lt;/o:p>&lt;/span>&lt;/p>&lt;p class="MsoPlainText" style="MARGIN: 2pt 0cm 2pt 17.85pt; TEXT-INDENT: -17.85pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Symbol; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol">&lt;span style="mso-list: Ignore">&amp;middot;&lt;span style="FONT: 7pt &amp;quot;Times New Roman&amp;quot;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span>&lt;/span>&lt;/span>&lt;strong style="mso-bidi-font-weight: normal">&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Transfert et installation &amp;agrave; bord du bateau. Nuit &amp;agrave; bord.&lt;o:p>&lt;/o:p>&lt;/span>&lt;/strong>&lt;/p>';
     
    $value = (String)$value;
    $value = html_entity_decode($value);
    echo strip_tags($value);
    ?>
    Et bien j'obtient le resultat suivant:

    · Rendez-vous des participants &agrave; l'a&eacute;roport.
    · Assistance aux formalit&eacute;s d'enregistrement et d'embarquement, puis d&eacute;collage &agrave; destination de Louxor. Collation servie &agrave; bord.
    · Accueil &agrave; l'arriv&eacute;e et assistance aux formalit&eacute;s d'entr&eacute;e.
    · Transfert et installation &agrave; bord du bateau. Nuit &agrave; bord.
    Ce qui parait conforme au resultat attendu...

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    495
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 495
    Par défaut
    oui le resultat que tu obtiens est tres correct.. Et c'est celui que j'esperais..

    Donc je suppose que ca vient d'un reglage du serveur qui est different de celui ou tu fais tes tests.. au niveau version, j'en suis la :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    PHP Version 5.2.0-8+etch3
    Mais bon, je n'ai pas la main niveau administration sur ce serveur.. donc bon.. je vais en rester au preg_replace.. j'ai utilisé une ptite astuce pour garder les retours a la ligne..

    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
    function html2txt($document){
    	$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
    	'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    	'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    	'@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
    	);
    	$text = preg_replace($search, '', $document);
    	return $text;
    }
     
    function myStripTags($value)
    {
    	$value = (String)$value;
    	$value = str_ireplace('&middot;', '-&nbsp;', $value);
    	$value = str_ireplace('<br>', '__br__', $value);
    	$value = str_ireplace('<br/>', '__br__', $value);
    	$value = str_ireplace('<br />', '__br__', $value);
    	$value = str_ireplace('</p>', '</p>__br__', $value);
    	$value = str_ireplace('</div>', '</div>__br__', $value);
    	$value = html2txt($value);
    	$value = str_ireplace('__br__', '<br />', $value);
    	$value = str_ireplace('&nbsp;', ' ', $value);
    	return $value;
    }
    ca marche bien.. Merci Mr. N.

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

Discussions similaires

  1. open avec comport
    Par Goldocrack dans le forum C++Builder
    Réponses: 6
    Dernier message: 15/04/2009, 08h12
  2. [HttpClient] comportement bizarre, saute des catch()...
    Par iubito dans le forum Développement Web en Java
    Réponses: 4
    Dernier message: 04/02/2004, 15h25
  3. [Sybase] Comportement bizarre d'une table
    Par sdozias dans le forum Sybase
    Réponses: 4
    Dernier message: 03/02/2004, 10h39
  4. [Free Pascal] Comportement de Exec
    Par néo333 dans le forum Free Pascal
    Réponses: 3
    Dernier message: 01/11/2003, 17h46
  5. Réponses: 2
    Dernier message: 22/09/2003, 11h23

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