Retirer le balisage HTML

Retirer le balisage HTML

Messagepar Michael DELIQUE » 14 Sep 2010 à 13:02

Code : Tout sélectionner
Function HTMLTagRemove(wChaine As String, wnbRemoveBreakRow As Boolean, wnbRemoveJavaScript As Boolean,wnbRemoveCSS As Boolean) As String
   
   Dim Chaine As String
   Dim vrValue As Variant
   Dim Texte As String
   Dim tbRemove(0 To 1)
   
   On Error Goto ErreurHandle
   
   HTMLTagRemove = ""
   
   If Trim(wChaine) = "" Then
      HTMLTagRemove = wChaine
      Exit Function
   End If
   
   If Instr(wChaine,"<") = 0 Then
      HTMLTagRemove = wChaine
      Exit Function
   End If
   
   Chaine = wChaine
   
   'retire les retour chariot
   If wnbRemoveBreakRow = True Then
      tbRemove(0) = Chr(10)
      tbRemove(1) = Chr(13)
      Chaine = Replace(Chaine,tbRemove,"")
      Erase tbRemove
   End If
   
   'retire les balise Js et le javascript contenu
   If wnbRemoveJavaScript = True Then
      If Instr(Lcase(Chaine),"<script") > 0 Then
         vrValue = Split(Chaine,"<script",,2)
         If Isempty(vrValue) = False Then
            If Isarray(vrValue) = True Then
               Chaine = ""
               Forall value In vrValue
                  Texte = Trim(Cstr(value))
                  If Texte <> "" Then
                     If Instr(Lcase(Texte),"script>") > 0 Then
                        Chaine= Chaine+Strrightback(Texte,"script>",1)
                     Else
                        Chaine= Chaine+Texte
                     End If
                  End If                  
               End Forall
            End If
         End If
      End If
      vrValue = Null
   End If
   
   'retire les balise style et tout le style contenu
   If wnbRemoveCSS= True Then
      If Instr(Lcase(Chaine),"<style") > 0 Then
         vrValue = Split(Chaine,"<style",,2)
         If Isempty(vrValue) = False Then
            If Isarray(vrValue) = True Then
               Chaine = ""
               Forall value In vrValue
                  Texte = Trim(Cstr(value))
                  If Texte <> "" Then
                     If Instr(Lcase(Texte),"style>") > 0 Then
                        Chaine= Chaine+Strrightback(Texte,"style>",1)
                     Else
                        Chaine= Chaine+Texte
                     End If
                  End If                  
               End Forall
            End If
         End If
      End If
      vrValue = Null
   End If
   
   'retire toute les balise restante
   vrValue = Split(Chaine,"<")
   If Isempty(vrValue) = False Then
      If Isarray(vrValue) = True Then
         Chaine = ""
         Forall value In vrValue
            Texte = Trim(Cstr(value))
            If Texte <> "" Then
               If Instr(Texte,">") > 0 Then
                  Chaine= Chaine+Strright(Texte,">")
               Else
                  Chaine = Chaine+Texte
               End If
            End If
         End Forall
      End If      
   End If
   vrValue = Null
   
   HTMLTagRemove = Chaine
   Chaine = ""
   
   Exit Function
ErreurHandle:
   Msgbox "("+Cstr(Getthreadinfo (1))+" Call by "+Cstr(Getthreadinfo(10))+")"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"
   HTMLTagRemove = ""
   Exit Function
End Function
Dernière édition par Michael DELIQUE le 14 Sep 2010 à 13:23, édité 1 fois.
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy

Messagepar Michael DELIQUE » 14 Sep 2010 à 15:00

AquaNotes a écrit:Une implémentation avec LS2J :


Biblio JAVA


[syntax="java"]import java.util.regex.*;

public class LSRegEx {

public static String stripTags (String s) {
Pattern p = Pattern.compile("<\\w+(\\s+("[^"]*"|'[^']*'|[^>])+)?>|</w>");
Matcher m = p.matcher(s);
return m.replaceAll("");
}

public static String stripScripts (String s) {

Pattern p = Pattern.compile("<script>]*>([\\S\\s]*?)</script>");
Matcher m = p.matcher(s);
return m.replaceAll("");
}

}[/syntax]


Agent LS

[syntax="LotusScript"] Dim jSession As JavaSession
Dim lSRegEx As JavaClass

Set jSession = New JavaSession()
Set lSRegEx = jSession.GetClass("LSRegEx")

Const codeHtml = |a <a>link</a><script>alert("hello world!");</script>|

Print lSRegEx.stripTags(codeHtml )
Print lSRegEx.stripScripts(codeHtml )
Print lSRegEx.stripTags(lSRegEx.stripScripts(codeHtml ) )
[/syntax]
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy

Messagepar Michael DELIQUE » 14 Sep 2010 à 15:02

Une version JavaScript
Code : Tout sélectionner

chaine.replace(/<\S[^><]*>/g, "");
chaine = chaine.replace(/[\n\b]/g,"");
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy


Retour vers World Wide Web (Web)