2

我在 PHP 中有一个巨大的字符串。其中有许多 url 字符串,例如:

background: #9dc9de url("@{base-url}/img/background.jpg") center top no-repeat;

遍历整个字符串的最佳方法是什么,找到任何 url 字符串并根据当前时间戳在扩展之前插入哈希码以获得结果,例如:

background: #9dc9de url("@{base-url}/img/background_013857308.jpg") center top no-repeat;

我目前正在尝试explode基于一些参数,但这绝对不是一个干净的想法。

4

2 回答 2

1

您不应该在生产服务器中使用这种方法,因为您每次都会调用所有文件,而不是从缓存中获取它们。但这对于开发服务器来说可能是一个好主意。

我会使用:

$tm = time();
$css = str_ireplace( array('.jpg', '.gif', '.png'), array('.jpg?t='.$tm, '.gif?t='.$tm, '.png?t='.$tm), $css );
于 2013-05-20T21:09:24.403 回答
0

我想出了preg_match_all('/\("([^"]+)"\)/', $content, $matches);匹配括号和双引号内的任何内容。

于 2013-05-20T21:15:04.850 回答