如何让preg_replace()删除两个标签之间的文本?

I'm trying to make a function in PHP that can delete code within two tags from all .js file within one folder and all its subfolders. So far everything works except preg_replace(). This is my code:

<?php

deleteRealtimeTester('test');

function deleteRealtimeTester($folder_path)
{
    foreach (glob($folder_path . '/*.js') as $file) 
    {
        $string = file_get_contents($file); 

        $string = preg_replace('#//RealtimeTesterStart(.*?)//RealtimeTesterEnd#', 'test2', $string);

        $file_open = fopen($file, 'wb');
        fwrite($file_open, $string);
        fclose($file_open);
    }

    $subfolders = array_filter(glob($folder_path . '/*'), 'is_dir');

    if (sizeof($subfolders) > 0)
    {
        for ($i = 0; $i < sizeof($subfolders); $i++)
        {
            echo $subfolders[$i];
            deleteRealtimeTester($subfolders[$i]);
        }
    }
    else
    {
        return;
    }
}

?>

As mentioned I want to delete everything inside these tags and the tags themselve:

//RealtimeTesterStart

//RealtimeTesterEnd

It is important that the tags contains the forward slashes and also that if a file contains multiple of these tags, only code from //RealtimeTesterStart to //RealtimeTesterEnd is deleted and not from //RealtimeTesterEnd to //RealtimeTesterStart.

I hope that someone can help me.

I'm assuming that //RealtimeTesterStart, //RealtimeTesterEnd and the code in between are on different lines? In PCRE . does NOT match newlines. You need to use the s modifier ( and you don't need the () unless you need the captured text for the replacement):

#//RealtimeTesterStart.*?//RealtimeTesterEnd#s

Also, look at GLOB_ONLYDIR for glob instead of array_filter. Also, also, maybe file_put_contents instead of fopen etc.

Maybe something like:

foreach (glob($folder_path . '/*.js') as $file) {
    $string = file_get_contents($file);
    $string = preg_replace('#//RealtimeTesterStart.*?//RealtimeTesterEnd#s', 'test2', $string);
    file_put_contents($file, $string);
}

foreach(glob($folder_path . '/*', GLOB_ONLYDIR) as $subfolder) {
    deleteRealtimeTester($subfolder);
}

You could also change your regex to use the [\s\S] character set which can be used to match any character, including line breaks.

So have the following

preg_replace('#\/\/RealtimeTesterStart[\s\S]+\/\/RealtimeTesterEnd#', '', $string);

This would remove the contents of //RealtimeTesterStart to //RealtimeTesterEnd and the tags themselves.