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 7 of 7
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,008
    Thanks
    59
    Thanked 10 Times in 10 Posts

    Question Regular expression help

    I'm cleaning up the remains of a virus


    How would I remove

    <iframe>junk</iframe>

    in the following code

    <iframe>bad code</iframe><iframe>junk</iframe>

    If I use the regex:

    <iframe.*bad code.*iframe>

    it matches

    <iframe>bad code</iframe><iframe>junk</iframe>

    not just the

    <iframe>junk</iframe> that I wanted.

    I hope that makes sense


  • #2
    Regular Coder
    Join Date
    Apr 2009
    Location
    somewhere over the rainbow
    Posts
    105
    Thanks
    2
    Thanked 14 Times in 14 Posts
    well your code is make sense
    you ask to start with <iframe. in the mid you want "bad code". and in the end you want iframe>.
    <iframe>bad code</iframe><iframe>junk</iframe>


    you need to get more specific on your regex if you want only bad code.
    <iframe>.*e<\/iframe>
    i ended it with "e</iframe>"
    match <iframe>bad code</iframe>

    or <iframe>j.*k<\/iframe>
    match <iframe>junk</iframe>

    or <iframe>junk<\/iframe>
    match <iframe>junk</iframe>


    ___________
    i had this virus too
    on the end of the page before the </body>
    Last edited by Eran; 05-01-2009 at 11:34 AM.

  • #3
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,008
    Thanks
    59
    Thanked 10 Times in 10 Posts
    thanks but the bad code can be anything


  • #4
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,008
    Thanks
    59
    Thanked 10 Times in 10 Posts
    I should be more specific, its the

    <iframe>junk</iframe> that I need to match


  • #5
    Regular Coder
    Join Date
    Apr 2009
    Location
    somewhere over the rainbow
    Posts
    105
    Thanks
    2
    Thanked 14 Times in 14 Posts
    what are you tring to do?
    you want to delete the bad code or junk?

  • #6
    Regular Coder
    Join Date
    Apr 2009
    Location
    somewhere over the rainbow
    Posts
    105
    Thanks
    2
    Thanked 14 Times in 14 Posts
    junk and bad code could be anything or there are some sort of a string that can ident the "junk"

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    PHP Code:
    <?php

    $text 
    '<iframe>bad code1</iframe><iframe>bad code2</iframe><iframe>bad code3</iframe><iframe>bad code4</iframe><iframe>bad code5</iframe><iframe>bad code6</iframe><iframe>junk</iframe>';

    function 
    reg($v){
      
    $v preg_replace("/\//","\/",$v);
      return 
    '/('.$v.')/';
    }

    preg_match_all("/(<iframe[^<]+<\/iframe>)/",$text,$matches);
    $re1 array_map("reg",$matches[1]);
    $re2 array_fill(0,count($re1),'$1');
    array_pop($re2);
    array_push($re2,'');

    $result preg_replace($re1,$re2,$text);

    print 
    $result;

    ?>
    best regards


  •  

    Posting Permissions

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