我有一个接近工作的正则表达式模式,但我仍然缺少一些东西。我只想换出<script>
标签的时间戳src
:
所以我想要这个:
stuff BEFORE...<script src='/assets/mySite-1238459846.js'></script>
stuff AFTER...
变成这样:
stuff BEFORE...<script src='/assets/mySite-0123456789.js'></script>
stuff AFTER...
这是切换时间戳的代码,但仅当 withsrc
不包含任何特殊字符或斜杠时,才$stringNotSpecial
有效:
$stringNotSpecial='mySite-1238459846.js'; //Works!
$stringSpecial='stuff BEFORE...<script src='/assets/mySite-0123456789.js'></script>
stuff AFTER...'; //DOESNT work :(
$timestamp='0123456789';
$pattern = '/mySite-[0-9]+.js/';
echo preg_replace($pattern, 'mySite-'.$timestamp.'.js', $stringNotSpecial);
echo '<br>';
echo echo preg_replace($pattern, 'mySite-'.$timestamp.'.js', $stringSpecial);