SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert the string

    Code:
    #!/usr/bin/perl
    while($line = <DATA>){
    
    @valid_entities= ('<a>','<abbr>','<acronym>','<br>');
    my %htmlenties = map { $_ =>1 } @valid_entities;
    #$line =~ s/(<\w*?>(?![^<\w*?>]*<\/\w*?>))/$1/g;
    #$line =~ s/<\w*>(?![^<\/\w*>]*>)/&lt;/g;
    $line =~  s/(<(\w*?)>)/exists $htmlenties{$1} ? $1 : defined ($2) ? "&lt;$2&gt;" : "&lt;"/eg;
    print $line;
    }
    
    __DATA__
    <helloe>How r u <a> www.google.com</a>
    <hi>How r u </hi><et,-2><><br/>


    How to convert character '<' to '&lt;'and '>' to &gt; for all the characters which is not present in the array and only if it doesnot have a closing end tag.In the above example,

    the output should look like


    Code:
    &lt;helloe&gt;How r u <a> www.google.com</a>
    <hi>How r u </hi>&lt;et,-2&gt;&lt;&gt;<br/>

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where di you get that code from? It appears to already be using a hash (&#37;htmlenties) to check for valid html tags and perform the escaping if necessary. But there is no %htmlenties in the code you posted.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is the hash to check whether string exists in an array.

    I want the regular expression to check whether the tag has the closing tag
    Code:
    while($line = <DATA>){
    $line =~ s/<(?![^<]*>)/&lt;/g;
    print $line;
    }
    
    __DATA__
    <helloe>How r u <a> www.google.com</a>
    <hi>How r u </hi><et,-2><
    For example, the above code checks for the ending '>'. If it doesnot exists it converts '<' to '&lt;'


    In similar way to how check for the tag if it has the closing tag.
    If it doesnot have, then convert < to &lt; and > to &gt;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •