0

我有一个接近工作的正则表达式模式,但我仍然缺少一些东西。我只想换出<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);
4

1 回答 1

1

尝试

$stringSpecial='stuff BEFORE...<script src=\'/assets/mySite-0123456789.js\'></script>stuff AFTER...';

或者

$stringSpecial='stuff BEFORE...<script src="/assets/mySite-0123456789.js"></script>stuff AFTER...';

http://ideone.com/Vq8TSS的输出-

result: Success     time: 0.01s    memory: 20520 kB     returned value: 0
input: no
output:

mySite-0123456789.js<br>stuff BEFORE...<script src='/assets/mySite-0123456789.js'></script>stuff AFTER...
于 2013-02-20T17:43:57.117 回答