Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Encoding HTML elements inside CODE tags

    I've been working on a RSS parser for a couple of months now. Everything is peachy so far, except for two issues. I'll discuss one in this thread, and the other one in another thread.

    When I parse through certain feeds, I come across <code> blocks, where the HTML code was written as HTML instead of being encoded. For example:
    Code:
    <code><span class="highlight">text</span></code>
    
    ... instead of the correct ...
    
    <code>&lt;span class="highlight"&gt;text&lt;/span&gt;</code>
    Assuming that there is one or more of these <code> blocks in a feed, how would I go about converting all code inside the <code> tags to their encoded values? (&lt; rather than <)

    I've tried the following already:
    PHP Code:
    $output_rss preg_replace("/<code>(.|\s)*<\/code>/i""<code>"htmlencode("\\0") ."</code>"); 
    ... but it keeps throwing errors. Any ideas? Any working code that I can deconstruct and learn from?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    when you want to perform actions on your matches try preg_replace_callback, its actually easier than it may at first seem.

    PHP Code:
    <?php
    $str 
    '
    <code>
        <span class="highlight">text innit</span>
    </code>
    <code>
        <span class="highlight">more text innit</span>
    </code>
    '
    ;

    function 
    encode$regs ){
        return 
    str_replace$regs[1] , htmlspecialchars$regs[1] ) , $regs[0] ) ;
    }
    echo 
    preg_replace_callback"|<code>(.*)<\/code>|Uis" 'encode' $str ) ;
    ?>
    that does not work for nested <code> tags but otherwise its ok
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    what about...

    PHP Code:
    $the_code "<code><span class=\"highlight\">some text</span></code> and some more text <code> and some <b>more code</b></code>";

    $find "/<code>(.)*<\/code>/si";

    $output_rss preg_replace_callback($find,"call_back_function",$the_code);

    function 
    call_back_function($matches) {
        
    $output htmlentities($matches[0]);
        
    $temp eregi_replace("&lt;code&gt;","<code>",$output);
        
    $temp eregi_replace("&lt;/code&gt;","</code>",$temp);
        
        return 
    $temp;
    }


    print 
    $output_rss
    ?

    -Celt

  • #4
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    PLEH to fp. same thing.


  •  

    Posting Permissions

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