php preg_replace comment blocks

前端 未结 2 1858
小鲜肉
小鲜肉 2020-12-20 06:22

the patern is like so

/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]         


        
2条回答
  •  温柔的废话
    2020-12-20 07:03

    This removes the comment blocks:

    preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string)
    

    And this get's rid of obsolete whitespace as well:

    preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string)
    

    Here's a test script:

    #!/usr/bin/php
    
    

    Output with PHP 5.3.4:

    /* comment [comment goes here] */
    /* comment please do not delete the lines below */
    [I am a special line so I should not be changed ]
    /* comment please do not delete the line above */
    ---
    
    
    [I am a special line so I should not be changed ]
    
    ---
    [I am a special line so I should not be changed ]
    

提交回复
热议问题