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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Removing comments

    I need to have PHP read another file, and do two things (at least there are two that I don't know how to do.)

    1) Delete anything after and including a // but only on that line. The line does not need to start with the // but if it exists, everything on the line after the // and including the // need to go.

    2) Delete anything inbetween and including a /* and */ on any number of lines.

    Basicly what I am doing is removing any comments from a JavaScript file. I am also replacing new lines and tabs with spaces, however I don't need help with that part of it.

    Thank you for your help in figuring this one out.
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I would use regex in the preg_replace function.

    This regex should work: /\/\*(.+)\*\//
    I'm not the best with this stuff so someone may have a better way.. I'm pretty sure that'll work though.. hope this helps!

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Your regex dosn't appear to do anything at all...

    PHP Code:
    $var = <<<HEREDOC
            var code = 'test'; // Remove me!
            /*
                Remove this too
            */
            var code = 'test';
    HEREDOC;

        
    print_rpreg_replace('/\/\*(.+)\*\//'''$var) ); 
    Returns:

    Code:
    var code = 'test'; // Remove me!
    /*
    	Remove this too
    */
    var code = 'test';
    And should return:

    Code:
    var code = 'test';
    var code = 'test';
    Did I miss a step?
    If I'm postin here, I NEED YOUR HELP!!

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Location
    Kolkata,India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    You'll need to different regexes, one for single line comments and one for multi-line comments.

    PHP Code:
    <?php
    $var 
    = <<<HEREDOC
            var code = 'test'; // Remove me!
            /*
                Remove this too
            */
            var code = 'test';
    HEREDOC;

        
    $var preg_replace('!//.+!'''$var);  
        
    $var preg_replace('!/\*.+\*/!s'''$var);  

        
    printf('<pre>%s</pre>',$var);
    ?>

  • #5
    New to the CF scene
    Join Date
    Aug 2007
    Location
    Kolkata,India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    A small fix here...

    PHP Code:
    <?php
    $var 
    = <<<HEREDOC
            var code = 'test'; // Remove me!
            /*
                Remove this too
            */

            if(test) { callem();/* one more here */ return 1;} // test ok
            /* hahahah */
            var code = 'test';
    HEREDOC;

        
    //printf('<pre>%s</pre>',$var);

        
    $var preg_replace('!//.+!'''$var);  
        
    $var preg_replace('!/\*[^/\\*]+\*/!s'''$var);  

        
    printf('<pre>%s</pre>',$var);
    ?>

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Thank you very much!
    If I'm postin here, I NEED YOUR HELP!!


  •  

    Posting Permissions

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